W3docs

How to get time difference in minutes in PHP

To get the time difference in minutes between two dates in PHP, you can use the DateTime class and the diff() method.

To get the time difference in minutes between two dates in PHP, you can use the DateTime class and the diff() method. Here's an example:

How to get the time difference in minutes between two dates in PHP?

<?php

$start = new DateTime('2022-01-01 10:00:00');
$end = new DateTime('2022-01-01 11:30:00');

$diff = $end->diff($start);
$minutes = ($diff->days * 24 * 60) + $diff->h * 60 + $diff->i;

echo $minutes; // output: 90

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

This will give you the number of minutes between the two dates. If you want the number of minutes as a decimal value, you can calculate it using the seconds property from the DateInterval object:

How to get the time difference in minutes as a decimal value in PHP?

<?php

$start = new DateTime('2022-01-01 10:00:00');
$end = new DateTime('2022-01-01 11:30:45');

$diff = $end->diff($start);
$total_minutes = ($diff->days * 24 * 60) + $diff->h * 60 + $diff->i + $diff->s / 60;

echo number_format($total_minutes, 2); // output: 90.75

You can also use the DateInterval::format() method with specifiers like %h (hours), %i (minutes), %s (seconds), %d (days), and %m (months) to display the difference in various units.

echo $diff->format('%d days, %h hours, %i minutes, %s seconds');

(Note: For PHP 8.1+, you can use $diff->totalMinutes to get the total difference in minutes directly.)