time difference in HH:MM:SS format

后端 未结 2 911
庸人自扰
庸人自扰 2021-01-27 23:31

I want to get time difference in HH:MM:SS format below is the code

if time diff in seconds it should display l like 00:00:35 In minutes :00:30:35 In Hrs :01:30:35

相关标签:
2条回答
  • 2021-01-28 00:10

    use

    $hours   = $interval->format('%H'); 
    $minutes = $interval->format('%I');
    $seconds = $interval->format('%S');
    

    and concat both three for one variable

    or use $interval->format('%H:%I:%S') for single output

    //output 00:30:35
    
    0 讨论(0)
  • 2021-01-28 00:12

    You can use strtotime() for time calculation. Here is an example:

    $time1 = strtotime('10:55:59');<br>
    $time2 = strtotime('10:56:00');<br>
    $diff = $time2 - $time1;<br>
    echo 'Time 1: '.date('H:i:s', $time1).'\n';<br>
    echo 'Time 2: '.date('H:i:s', $time2).'\n';
    
    if($diff){<br>
        echo 'Diff: '.date('H:i:s', $diff);<br>
    }else{<br>
        echo 'No Diff.';<br>
    }<br>
    

    Output:

    Time 1: 09:00:59<br>
    Time 2: 09:01:00<br>
    Diff: 00:00:01
    
    0 讨论(0)
提交回复
热议问题