PHP - time minus time to minutes

前端 未结 2 1386
一整个雨季
一整个雨季 2021-01-06 04:29

In php i have two times - 11:00:00 and 12:45:00. I want to get the difference between them in minutes, in this case 105 minutes. Whats the best way that can be done?

相关标签:
2条回答
  • 2021-01-06 05:05

    Here you go:

    ( strtotime('12:45:00') - strtotime('11:00:00') ) / 60
    

    strtotime() is a very useful function. It returns the Unix timestamp for a wide variety of times and dates. So, if you take the two timestamps, and subtract them, then you have the difference in seconds. Divide by 60 to get the minutes.

    0 讨论(0)
  • 2021-01-06 05:07
        $time_diff = strtotime('2013-03-13 12:45:00') - strtotime('2013-03-13 11:00:00');
        echo $time_diff/60;
    

    I just kept dates as not sure if I keep the time part only it would return the correct diff or not.

    EDIT

    I just tested it works without date too ...

        $time_diff = strtotime('12:45:00') - strtotime('11:00:00');
        echo $time_diff/60;
    

    So to answer you question - strtotime() returns a timestamp (the number of seconds since January 1 1970 00:00:00 UTC) so you simply divide it by 60 to convert it result into minutes.

    0 讨论(0)
提交回复
热议问题