I had my own way of doing this:
<?php
function timeDiff( $start, $end )
{
$seg = (int) substr( $end, 0, 2 );
$add = $seg > 23
? ( $seg - 23 ) * 3600
: 0;
if( $seg > 23 )
$end = substr_replace( $end, '23', 0, 2 );
$time1 = strtotime($start);
$time2 = strtotime($end) + $add;
$diff = $time2 - $time1;
$hour = floor( $diff / (60 * 60) );
$minute = $diff - $hour * (60 * 60);
$minute = floor( $minute / 60 );
$hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
$minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
return $hour . ':' . $minute;
}
$start = "02:40:00";
$end = "34:20:00";
echo timeDiff( $start, $end );
And if you also want to calculate the seconds, then it is slightly modified:
<?php
function timeDiff( $start, $end )
{
$seg = (int) substr( $end, 0, 2 );
$add = $seg > 23
? ( $seg - 23 ) * 3600
: 0;
if( $seg > 23 )
$end = substr_replace( $end, '23', 0, 2 );
$time1 = strtotime($start);
$time2 = strtotime($end) + $add;
$diff = $time2 - $time1;
$hour = floor( $diff / (60 * 60) );
$minute = $diff - $hour * (60 * 60);
$minute = floor( $minute / 60 );
$second = $diff - ( $hour * 60 * 60 ) - ( $minute * 60 );
$hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
$minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
$second = str_pad( $second, 2, '0', STR_PAD_LEFT );
return $hour . ':' . $minute . ':' . $second;
}
$start = "02:40:00";
$end = "34:20:45";
echo timeDiff( $start, $end );
source
share