Jump to content

Calculate Hours & Minutes between two Dates/Time


julzk

Recommended Posts

Hi,

 

I am wanting to Calculate Hours & Minutes between two Dates/Time.

 

I have four fields: ss_datestart, ss_dateend and ss_timestart and ss_timeend.

 

I want to display the hours and minutes between two dates/times.

 

Example:

 

$ss_datestart $ss_timestart --- $ss_dateend $ss_timeend

05-12-2009 13:44:31 --- 05-12-2009 17:55:31

 

I want it to then display the difference in hours and minutes only, so it would return: 04h 11m (ignoring the seconds of course).

 

 

And if it were something that went over a day so:

 

05-12-2009 13:44:31 --- 06-12-2009 17:55:31 <--- This would return: 28h 11m

 

 

FYI: Dates are dd-mm-yyyy

There might be an easier way, but something like this should do the trick off the top of my head.

 

<?php
$day1 = "05-12-2009 13:44:31";
$day2 = "05-12-2009 17:55:31";

$result = strtotime($day1) - strtotime($day2);
$diff = $result / 31556926;
$h = date("h",$diff);
$i = date("i",$diff);
$h = (substr($h,0,1) == 0) ? substr($h,1) : $h;
$i = (substr($i,0,1) == 0) ? substr($i,1) : $i;

echo "$h hours $i minutes";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.