How to Calculate the Difference Between Two Dates in PHP
In PHP, use $from->diff($to)->days to get the total number of days between two dates. The difference from 2022-01-01 to 2022-03-31 is 89 days, or 90 if you count both dates. Use DateInterval’s invert property when the order should determine the sign.
Get the difference in days with diff()
With date-only inputs in the same time zone, the two values in this example represent midnight on their respective dates. Create DateTimeImmutable objects and call diff(), or use the equivalent date_diff() function.
<?php
$timezone = new DateTimeZone('UTC');
$from = new DateTimeImmutable('2022-01-01', $timezone);
$to = new DateTimeImmutable('2022-03-31', $timezone);
$interval = $from->diff($to);
echo $interval->days, PHP_EOL;
echo date_diff($from, $to)->days, PHP_EOL;
echo $interval->m, ' ', $interval->d, PHP_EOL;
Save each example as its own PHP file and run it with a command such as php date-difference.php. The first two lines each show 89. The third shows 2 30, meaning two months and 30 days.
The days property of a DateInterval from diff() gives the total number of days. The d property is the day component after years and months. For objects created by another method, such as new DateInterval(...), days can be false.
Keep the sign when the dates are reversed
When the end date is earlier, days still gives the magnitude, 89. The direction is in invert, which is 1 when the end comes before the start.
<?php
$timezone = new DateTimeZone('UTC');
$from = new DateTimeImmutable('2022-03-31', $timezone);
$to = new DateTimeImmutable('2022-01-01', $timezone);
$interval = $from->diff($to);
$signedDays = $interval->invert ? -$interval->days : $interval->days;
echo $signedDays, PHP_EOL;
echo $interval->format('%r%a'), PHP_EOL;
Both lines print -89. The $signedDays value is an integer for calculations. With DateInterval::format(), %r%a produces a string containing the total days and a minus sign only when negative. Calling diff($to, true) sets invert to 0 for an absolute result.
Count the start date and end date
To count both dates, as in an attendance period, add 1 to the difference between date-only values in the same time zone. This example passes dates without times to the DateTimeImmutable constructor and treats an end date before the start as an error.
<?php
$timezone = new DateTimeZone('UTC');
$start = new DateTimeImmutable('2022-01-01', $timezone);
$end = new DateTimeImmutable('2022-03-31', $timezone);
if ($end < $start) {
throw new InvalidArgumentException('End date precedes start date.');
}
echo $start->diff($end)->days + 1, PHP_EOL;
echo $start->diff($start)->days + 1, PHP_EOL;
| Counting rule | Days |
|---|---|
| Difference from January 1 to March 31 | 89 |
| Both dates included | 90 |
| One date counted inclusively | 1 |
The first line is 90 and the second is 1. Adding 1 counts calendar dates. For elapsed time between timestamps, use the approach below.
Distinguish calendar days from elapsed time
A calendar day can differ from 24 hours where clocks change for daylight saving. To measure the actual time between midnight on March 10, 2024, and the next midnight in New York, subtract the Unix timestamps returned by getTimestamp().
<?php
$timezone = new DateTimeZone('America/New_York');
$start = new DateTimeImmutable('2024-03-10 00:00:00', $timezone);
$end = new DateTimeImmutable('2024-03-11 00:00:00', $timezone);
$seconds = $end->getTimestamp() - $start->getTimestamp();
echo $start->diff($end)->days, PHP_EOL;
echo $seconds, PHP_EOL;
echo $seconds / 3600, PHP_EOL;
The example prints 1 day, 82800 seconds, and 23 hours. The dates are one calendar day apart, and the clock moves forward by one hour during the interval.
Use a common reference time zone for calendar comparisons and subtract timestamps for elapsed time in seconds. The timestamp method shown here assumes inputs at second precision.



