Search code examples
phpstringtimedateinterval

subtract two times in php and laravel 24H


This seems like a fairly simple question, but I'm having trouble with it! In my request, I have two fields

$takeOffTime = '23:00';
$landingTime = '01:05';

I want to subtract breakout from break in to get the difference, and then take that number and subtract it from the difference between takeOffTime and landingTime.

How can I do this in 24H formt 'h:i'?

$takeOff = '23:00';
$landing = '01:10';

$t1 = strtotime($takeOff);
$t2 = strtotime($landing);

Solution

  • You can subtract two times to H:i format like this.

    $takeOff = '23:00';
    $landing = '01:10';
    
    $t1 = strtotime($takeOff);
    $t2 = strtotime($landing);
    
    $diff = gmdate('H:i', $t2 - $t1);
    
    dd($diff); // "02:10"