Hmm... worthy of a double post... here's the updated code to give you the hour difference if the time falls over a day:
<?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;
?>
As always: Working Example
~Brett