How to get time difference in minutes in PHP?

Spread the love

Sometimes, we want to get time difference in minutes in PHP.

In this article, we’ll look at how to get time difference in minutes in PHP.

How to get time difference in minutes in PHP?

To get time difference in minutes in PHP, we can use the diff method.

For instance, we write

$start_date = new DateTime('2022-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2023-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>';

to define 2 DateTime objects.

Then we call $start_date->diff to get the difference between start_date and the datetime in the argument.

And then we get the minutes difference with $since_start->m.

Conclusion

To get time difference in minutes in PHP, we can use the diff method.

Leave a Reply

Your email address will not be published.