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′
Advertisement
Answer
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;