Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

Time difference in hours and minutes PHP

Struggling to workout how best to finish off my project on time and PHP.

Currently have:

function getTimeDiff($startTime){


// no idea what ill need to work the difference out yet


}

$meetTimes = array(


                      "day" => "Monday",
                      "start_time" => "1416005281",
                      "finish_time" => "1416005281",
                      "room" => "5"


);



foreach ($meetTimes as $meeting){


if(getTimeDiff( $meeting['start_time']) < 1 hour){

echo '<h5>XX minutes from now</h5>';

} elseif (getTimeDiff( $meeting['start_time']) < 1 hour){

echo '<h5>XX hour XX minutes from now</h5>';

}
elseif (getTimeDiff( $meeting['start_time']) >= 2 hours){

echo '<h5>XX hours XX minutes from now</h5>';

}

elseif (getTimeDiff( $meeting['start_time'])  ? ){

echo '<h5>XX minutes ago</h5>';

}

 elseif (getTimeDiff( $meeting['start_time'])   ? ){

echo '<h5>XX hour XX minutes ago</h5>';

}
elseif (getTimeDiff( $meeting['start_time'])  ? ){

echo '<h5>XX hours XX minutes ago</h5>';

}


echo '<h5>' . date('H:i', $meeting['start_time']). '</h5>';
echo '<h5>' . $meeting['room'] . '</h5>';


}

I now want to add in how long till the meeting is due to take place up to 24 hours.

I am stuck on how I would does this though and be able to echo it out for each meeting.

Example:

Time now is 12:00pm but of course I would need to use time() to get the current time in my project but for the sake this example lets call it 12:00pm.

The first meeting today is at 12:45pm:

45 minutes from now.

12:45pm

Room 5

The second meeting today is at 13:45pm:

1 hour 45 minutes from now.

13:45pm

Room 10

Last on the list would be a meeting that took place yesterday at 12:00pm replacing "from now" to "ago":

24 hours 00 minutes ago.

12:00pm

Room 10

I know I am going to need some sort of function to work this out on the fly but I am struggling to see how I would does this and include it in my foreach loop above.

Then inside my foreach loop I also need to work out how long has past since the meeting finished, not sure how to do that either, is there an easier way to achieve this?

Ive read the manual and ive looked at few things on stack but I cant make heads or tails of it.

1 Answer

You should convert all of your times to DateTime objects. This will give you more control over the times and display values.

Robert Walker
Robert Walker
17,146 Points

Not sure how to do this at all.

I currently have the function:

function getTimeDiff($time){



// $time comes in as a unix timestamp 12883333443 etc


$start_date = new DateTime('Y-m-d H:i:s', $time);

$since_start = $start_date->diff(new DateTime('NOW'));



}

This doesn't work at all but reading over the manual I dont see how this is done there either.

Just to update ive tried this too:

function getTimeDiff($time){




$start_date = new DateTime("@$time");
$since_start = $start_date->diff(new DateTime('NOW'));


$r = $since_start->format('%h hours %i minutes');
return $r;

}

This works but it doesnt seem to be the cleanest way or even give me a lot of options if I want to format it differently.

It also limits me how I can tell if its 12 hours ago or in 12 hours.