Thanks for the link. I looked at the function and I was wondering how does it handle the meridiem? If I pass a hour:min:sec into it, how does it know/handle if it is AM or PM?
If they select 9:00 am(From) 12:45 pm(To)
2006-02-13 thru 2006-02-14 I need the result to be 7.30 (hours.mins)
3 hours and 45 min X 2 days
In my application, they can choose not only the From and start times, the meridiem but a date range as well. I am not sure how to implement this into what I need because the results would be greatly different it the To time was AM
Any advice on this would be great. Thank you again for your time.
Below is your function
<?php
function timeDiff($timebegin, $timefinish, $rtn=TRUE)
{
if(!is_numeric($timebegin))
{
// $timebegin is NOT a UNIX timesamp, so we need to make one
list($s_hr, $s_min, $s_sec) = explode(':', $timebegin);
$start = mktime($s_hr, $s_min, $s_sec, 1,1,1999);
}
else
{
// $timebegin is numerical, assume it's a timestamp:
$start = $timebegin;
}
if(!is_numeric($timefinish))
{
// $timefinish is NOT a UNIX timestamp, so we need to make one
list($f_hr, $f_min, $f_sec) = explode(':', $timefinish);
$end = mktime($f_hr, $f_min, $f_sec, 1,1,1999);
}
else
{
// $timefinish is numerical, assume it's a timestamp:
$end = $timefinish;
}
if($start <= $end)
{
$diff = $end-$start;
$dif['hour'] = floor( $diff/(60*60) );
$diff = $diff-($dif['hour']*(60*60));
$dif['min'] = floor( $diff/60 );
$dif['sec'] = $diff-($dif['min']*60);
$difference = $dif['hour'].' hours '.$dif['min'].' minutes '.$dif['sec'].' seconds';
}
else
{
// Dealing with an over-the day time period...
$mid = mktime(0,0,0,1,2,1999);
$dif1 = $mid-$start;
$dif2 = $end-$mid;
// The first time-period
$dif[1]['hour'] = floor( $dif1/(60*60) );
$dif1 = $dif1-($dif[1]['hour']*(60*60));
$dif[1]['min'] = floor( $dif1/60 );
$dif[1]['sec'] = $$dif1-($dif[1]['min']*60);
// The second time-period
$dif[2]['hour'] = floor( $dif2/(60*60) );
$dif2 = $dif2-($dif[2]['hour']*(60*60));
$dif[2]['min'] = floor( $dif2/60 );
$dif[2]['sec'] = $dif2-($dif[2]['min']*60);
// Now combine them to get the total time
$dif['tot']['hour'] = $dif[1]['hour']+$dif[2]['hour'];
$dif['tot']['min'] = $dif[1]['min']+$dif[2]['min'];
$dif['tot']['sec'] = $dif[1]['sec']+$dif[2]['sec'];
// Set the difference variable:
$difference = $dif['tot']['hour'].' hours '.$dif['tot']['min'].' minutes '.$dif['tot']['sec'].' seconds';
}
if($rtn == TRUE)
{ return $difference; }
else
{ echo $difference; }
}
$difference = timeDiff(mktime(12,54,00,1,1,1999), mktime(18,54,00,1,1,1999));
echo 'The difference between '.date('H:i:s', mktime(12,54,00,1,1,1999)).' and '.date('H:i:s', mktime(18,54,00,1,1,1999)).' is:<br>'.$difference;
$difference = timeDiff('22:00:00', mktime(01,00,00,1,2,1999));
echo '<hr>The difference between 22:00:00 and '.date('H:i:s', mktime(01,00,00,1,1,1999)).' is:<br>'.$difference;
?>