Get time difference in minutes in PHP
This article demonstrates how to get the time difference between two dates in minutes in PHP.
1. Using DateTime::diff() function
You can find the time difference between two dates using the diff() method of the DateTime class. The idea is to parse the given dates into DateTime objects and find the difference between the two with DateTime::diff(). It returns a DateInterval object with the properties y, m, d, h, i, s, and f to store the number of years, months, hours, minutes, seconds, and microseconds. It is recommended to set the time zone to get consistent results.
|
1 2 3 4 5 6 7 |
<?php $start = new DateTime("2016/10/01 04:00:00", new DateTimeZone('UTC')); $end = new DateTime("2020/12/26 22:20:20", new DateTimeZone('UTC')); $interval = $start->diff($end); print_r($interval); ?> |
The above code will output:
DateInterval Object
(
[y] => 4
[m] => 2
[d] => 25
[h] => 18
[i] => 20
[s] => 20
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 1547
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
You may also use the procedural style function date_diff(), which is an alias of DateTime::diff().
|
1 2 3 4 5 6 7 |
<?php $start = date_create("2016/10/01 04:00:00", timezone_open('UTC')); $end = date_create("2020/12/26 22:20:20", timezone_open('UTC')); $diff = date_diff($end, $start); print_r($diff); ?> |
The days property of the DateInterval object returns the total number of full days between the two dates. You can use it with the h and i properties to get the total number of minutes that passed between the two dates.
|
1 2 3 4 5 6 7 8 9 |
<?php $start = new DateTime("2016/10/01 04:00:00", new DateTimeZone('UTC')); $end = new DateTime("2020/12/26 22:20:20", new DateTimeZone('UTC')); $interval = $start->diff($end); $minutes = ($interval->days * 24 * 60) + ($interval->h * 60) + $interval->i; echo $minutes; // 2228780 ?> |
If you need to measure the elapsed time in minutes, you can get the timestamp between two instances in the script. This can be done using the time() function, which returns the number of seconds elapsed since the Unix Epoch.
|
1 2 3 4 5 6 7 8 |
<?php $start = time(); sleep(72); // sleep for 72 seconds $end = time(); $minutes = ($end - $start) / 60; echo $minutes; // 1.2 mins ?> |
2. Using strtotime() function
Alternatively, you can convert the given dates to the number of seconds elapsed since the Unix Epoch, and then find their difference. This can be done using the strtotime() function, which return a Unix timestamp corresponding to the specified date.
|
1 2 3 4 5 6 7 8 9 10 |
<?php date_default_timezone_set('UTC'); $start = strtotime("2016/10/01 04:00:00"); $end = strtotime("2020/12/26 22:20:20"); $minutes = floor(($end - $start) / 60); echo $minutes; // 2228780 ?> |
That’s all there is to getting the time difference between two dates in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)