How to calculate minute difference between two date-times in PHP?
Subtract the past most one from the future most one and divide by 60.
Times are done in Unix format so they're just a big number showing the number of seconds from
January 1, 1970, 00:00:00 GMT
The answers above are for older versions of PHP. Use the DateTime class to do any date calculations now that PHP 5.3 is the norm. Eg.
$start_date = new DateTime('2007-09-01 04:10:58'); $since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00')); echo $since_start->days.' days total<br>'; echo $since_start->y.' years<br>'; echo $since_start->m.' months<br>'; echo $since_start->d.' days<br>'; echo $since_start->h.' hours<br>'; echo $since_start->i.' minutes<br>'; echo $since_start->s.' seconds<br>';
$since_start is a DateInterval object. Note that the days property is available (because we used the diff method of the DateTime class to generate the DateInterval object).
The above code will output:
1837 days total
To get the total number of minutes:
$minutes = $since_start->days * 24 * 60; $minutes += $since_start->h * 60; $minutes += $since_start->i; echo $minutes.' minutes';
This will output:
Which is the actual number of minutes that has passed between the two dates. The DateTime class will take daylight saving (depending on timezone) into account where the "old way" won't. Read the manual about Date and Time http://www.php.net/manual/en/book.datetime.php