I have this data: $t1 = '75:00'; //Corresponds to Hours:Minutes $t2 = '05:13';
// I need to know the time diference in this example must return: '69:47'
I have this data: $t1 = '75:00'; //Corresponds to Hours:Minutes $t2 = '05:13';
// I need to know the time diference in this example must return: '69:47'
You can use the below code to calculate time difference -
$t1 = new DateTime('23:00');
$t2 = new DateTime('05:13');
$interval = $t1->diff($t2);
echo $interval->format("%H:%i");
*75:00 is not a valid time. You have to take care of that.
75:00 is not a valid time, but if you found yourself in a situation that you have to use that, use this code below
<?php
function convertToMinutes($time)
{
list($hour, $minutes) = explode(":", $time);
$hoursToMinutes = $hour * 60;
$addMinutes = $hoursToMinutes + $minutes;
return $addMinutes;
}
function convertToHours($time)
{
$hours = floor($time / 60);
$minutes = $time % 60;
if ($time < 60) {
return $time;
}
return $hours . ":" . $minutes;
}
function timeDifference($time1, $time2)
{
if ($time1 >= $time2) {
$diff = convertToMinutes($time1) - convertToMinutes($time2);
return convertToHours($diff);
}
$diff = convertToMinutes($time2) - convertToMinutes($time1);
return convertToHours($diff);
}
$t1 = '75:00';
$t2 = '05:13';
$answer = timeDifference($t1, $t2);
echo $answer;