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. Here's an example:

<?php

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

$diff_in_seconds = $end->getTimestamp() - $start->getTimestamp();
$minutes = floor($diff_in_seconds / 60);

echo $minutes; // output: 90

Watch a course Learn object oriented PHP

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 use the format() method to get the number of minutes as a decimal value:

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

$diff_in_seconds = $end->getTimestamp() - $start->getTimestamp();
$minutes = floor($diff_in_seconds / 60);
$seconds = $diff_in_seconds % 60;

$formatted_diff = sprintf('%d.%02d', $minutes, $seconds);

echo $formatted_diff; // output: 90.00

You can also use the h (hours), d (days), and m (months) format codes to get the time difference in those units.