HOWTO · PHP

How to Get Time Difference in Minutes in PHP

Learn how to calculate elapsed minutes and calendar date differences in PHP with DateTimeImmutable, date_diff(), and strtotime().

On this page

To get the elapsed time between two PHP date-time values in minutes, subtract their Unix timestamps and divide the seconds by 60. Choose this approach when you need a duration. Use date_diff() when you need a calendar interval, and use strtotime() when the inputs are parseable strings. Keep both values in the same timezone context, and decide whether the answer should retain its direction or be an absolute magnitude.

Calculate elapsed minutes from timestamps

DateTimeImmutable makes the input values explicit and avoids changing either value while calculating. Set a timezone on both values; the example uses UTC. Subtracting timestamps gives signed elapsed seconds, so a later end value produces a positive result and the sign follows the order of subtraction. Wrap the difference in abs() only when direction does not matter. Dividing by 60 preserves fractional minutes; use a rounding function only if the application requires whole minutes.

Run this example:

<?php
$timezone = new DateTimeZone('UTC');
$start = new DateTimeImmutable('2024-06-16 09:15:00', $timezone);
$end = new DateTimeImmutable('2024-06-16 10:00:00', $timezone);

$elapsedMinutes = ($end->getTimestamp() - $start->getTimestamp()) / 60;
echo "Elapsed minutes: $elapsedMinutes\n";

For actual elapsed time across daylight-saving changes, use timestamps from date-time values with the intended timezone. Do not assume that every calendar day contains exactly 1,440 elapsed minutes in a timezone that observes daylight saving.

Use date_diff() for calendar date differences

date_diff() (also available as $start->diff($end)) returns a DateInterval, not a single count of elapsed minutes. Its day, hour, and minute components are useful when the question is phrased in calendar terms, such as “how many days, hours, and minutes apart?” The interval’s invert property indicates direction when the end precedes the start; the absolute argument can suppress that direction.

<?php
$timezone = new DateTimeZone('UTC');
$start = new DateTimeImmutable('2024-06-16 09:15:00', $timezone);
$end = new DateTimeImmutable('2024-06-18 12:30:00', $timezone);

$interval = date_diff($start, $end);
echo $interval->format('%R%a days, %h hours, and %i minutes');

For a calendar interval, you can form a nominal minute count from its components with $interval->days * 1440 + $interval->h * 60 + $interval->i. That combines calendar days and clock components; it is not a replacement for timestamp subtraction when you need exact elapsed minutes across timezone transitions. If direction matters, retain the interval’s sign or apply it to the calculated magnitude using $interval->invert.

Parse time strings with strtotime()

strtotime() converts a parseable date-time string to a Unix timestamp. A time-only string is interpreted relative to a date context, so provide full dates when values may cross midnight or belong to different dates. Set the default timezone explicitly (or include a timezone in each string) so both timestamps use the intended context. The subtraction below keeps the result signed; use abs() for an unsigned duration.

<?php
date_default_timezone_set('UTC');
$start = strtotime('2024-06-16 23:30:00 UTC');
$end = strtotime('2024-06-17 00:15:00 UTC');

$minutes = ($end - $start) / 60;
echo "Elapsed minutes: $minutes\n";

When parsing untrusted or variable input, check that each call returned a timestamp before subtracting; parsing can fail and return false. For application data that needs clear timezone handling, prefer constructing DateTimeImmutable values with an explicit timezone. Use strtotime() for concise parsing, timestamp subtraction for elapsed duration, and date_diff() when the calendar interval itself is what you need to describe.