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.

Download  Run Code

 
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().

Download  Run Code

 
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.

Download  Run Code

 
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.

Download Code

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.

Download  Run Code

That’s all there is to getting the time difference between two dates in PHP.