-3

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'

Jose Ruiz
  • 3
  • 1
  • Here is a post that could be useful for your question: https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Marcello Perri Oct 09 '20 at 09:20
  • 1
    75:00 is not a valid time. Seperate it. `list($min, $sec) = explode(':', $t1);`. Then you can turn it all into seconds, diff that and calc back to minutes and seconds. – Markus Zeller Oct 09 '20 at 09:27
  • Something this simple doesn’t need a massive amount of date functions thrown at it, you can do the minimal amount of math required yourself … https://3v4l.org/O24qT – 04FS Oct 09 '20 at 09:28

2 Answers2

0

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.

Shubham Baranwal
  • 2,492
  • 3
  • 14
  • 26
0

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;
KINGSLEY OKPARA
  • 549
  • 1
  • 5
  • 16