PHP - time minus time to minutes

In php, I have two times - 11:00:00 and 12:45:00. I want to get the difference between them in a matter of minutes, in this case 105 minutes. How can this be done?

Thanks!

+4
source share
2 answers

Here you go:

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

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

+11
source
  $time_diff = strtotime('2013-03-13 12:45:00') - strtotime('2013-03-13 11:00:00'); echo $time_diff/60; 

I just set the dates as not sure if saving the temporary part will return the correct diff or not.

EDIT

I just checked that it works without a date too ...

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

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

+4
source

Source: https://habr.com/ru/post/1469034/


All Articles