Call $start->diff($end) on two DateTimeImmutable objects. It returns a DateInterval whose ->days is the total number of days and whose y, m, d, h, i and s fields hold the parts. For a total in minutes or hours, subtract the timestamps instead: intdiv($end->getTimestamp() - $start->getTimestamp(), 60).
PHP's date classes do the calendar math for you: leap years, months of different lengths and daylight saving time. The one thing to get straight is which number you want. A DateInterval holds the difference as parts (2 years, 2 months, 5 days), its days property holds the total days, and a total in minutes or hours comes from the timestamps. The sections below cover each, why the time zone changes the answer, comparing two dates, counting business days and computing an age, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.
1DateTimeImmutable::diff and DateIntervalRecommended
diff() works on both DateTime and DateTimeImmutable and returns a DateInterval. Read the parts from its fields, or let format() build the text: %y, %m and %d are the years, months and days parts, %a is the total number of days, and %R prints the sign. The interval is always stored as positive numbers; its invert field is 1 when the second date comes before the first. The procedural form, date_diff($a, $b), is the same function.
<?php $start = new DateTimeImmutable('2024-01-15 09:30:00'); $end = new DateTimeImmutable('2026-03-20 17:45:10'); $interval = $start->diff($end); // The parts: years, months, days, hours, minutes, seconds echo $interval->y, ' years, ', $interval->m, ' months, ', $interval->d, " days\n"; echo $interval->h, ' h ', $interval->i, ' min ', $interval->s, " s\n"; // The total number of whole days between the two echo $interval->days, " days in total\n"; // format() builds the text; %a is the total days, %d only the part echo $interval->format('%y years, %m months, %d days'), "\n"; echo $interval->format('%a days, %H:%I:%S'), "\n"; // The order matters: invert is 1 when the end is before the start $back = $end->diff($start); echo 'invert: ', $interval->invert, ' / ', $back->invert, "\n"; echo $interval->format('%R%a days'), ' / ', $back->format('%R%a days'), "\n"; // Pass true as the second argument to always get a positive interval echo $end->diff($start, true)->format('%R%a days'), "\n"; // Just the number of days between two dates $days = (new DateTimeImmutable('2026-01-01')) ->diff(new DateTimeImmutable('2026-12-25')) ->days; echo "$days days\n";
Output
The parts read 2 years, 2 months, 5 days and 8 h 15 min 10 s, while ->days gives 795 days in total. Mixing these up is the most common bug: %d prints 5, the day part, and %a prints 795. Swapping the dates sets invert: 0 / 1, which %R shows as +795 days and -795 days; with true as the second argument the result is always +. The last line, 358 days, is the usual "days between two dates" answer. To print the dates themselves in another layout, see formatting a date.
2Total minutes, hours or seconds
A DateInterval has no "total minutes" field. Its i property is only the minutes part, between 0 and 59, so $interval->i is wrong as soon as the gap is longer than an hour. The reliable way is to subtract the two Unix timestamps, which gives the elapsed seconds, and divide: intdiv() for whole units, / with round() for a decimal. On code that still passes date strings around, strtotime() gives the same timestamps.
<?php $start = new DateTimeImmutable('2026-03-02 08:15:00'); $end = new DateTimeImmutable('2026-03-04 11:40:30'); // Seconds between the two moments, then divide $seconds = $end->getTimestamp() - $start->getTimestamp(); echo "seconds: $seconds\n"; echo 'minutes: ', intdiv($seconds, 60), "\n"; echo 'hours: ', intdiv($seconds, 3600), "\n"; echo 'hours (decimal): ', round($seconds / 3600, 2), "\n"; // Hours, minutes and seconds, like a stopwatch printf("%d:%02d:%02d\n", intdiv($seconds, 3600), intdiv($seconds % 3600, 60), $seconds % 60); // The trap: the interval's i field is only the minutes part $i = $start->diff($end); echo 'i field: ', $i->i, " (not the total)\n"; // The old way, with date strings and strtotime() $a = strtotime('2026-03-02 08:15:00'); $b = strtotime('2026-03-04 11:40:30'); echo 'strtotime: ', intdiv($b - $a, 60), " minutes\n";
Output
The gap is 185130 seconds, which is 3085 minutes, 51 whole hours or 51.43 hours, and 51:25:30 as a stopwatch. The interval's i field says 25: just the minutes left over after the days and hours. You can rebuild the total from the parts (days * 1440 + h * 60 + i), but in a zone with daylight saving time a day is not always 24 hours, as the next section shows, so the timestamps are the safer source.
3Time zones and daylight saving time
A date string with no zone is read in the script's default zone, set by date.timezone in php.ini or by date_default_timezone_set(). Your laptop and your server can disagree, and in a zone with daylight saving time the answer changes: the day the clocks move forward has 23 hours. diff() counts calendar days and wall-clock time, while timestamps count elapsed seconds. Pass a DateTimeZone when you build the dates so the result doesn't depend on the server.
<?php $ny = new DateTimeZone('America/New_York'); // US clocks moved forward one hour on 8 March 2026 at 2:00 $start = new DateTimeImmutable('2026-03-07 12:00', $ny); $end = new DateTimeImmutable('2026-03-08 12:00', $ny); $i = $start->diff($end); echo 'diff: ', $i->days, ' day, ', $i->h, " hours\n"; $seconds = $end->getTimestamp() - $start->getTimestamp(); echo 'elapsed: ', $seconds / 3600, " hours\n"; // Dividing seconds by 86400 to get days breaks on this date echo 'seconds / 86400: ', round($seconds / 86400, 4), "\n"; echo 'intdiv(seconds, 86400): ', intdiv($seconds, 86400), "\n"; // The same wall-clock times in UTC: no DST, 24 hours $utc = new DateTimeZone('UTC'); $a = new DateTimeImmutable('2026-03-07 12:00', $utc); $b = new DateTimeImmutable('2026-03-08 12:00', $utc); echo 'UTC elapsed: ', ($b->getTimestamp() - $a->getTimestamp()) / 3600, " hours\n"; // Two zones: diff() compares the instants, not the clock times $x = new DateTimeImmutable('2026-06-01 09:00', $ny); $y = new DateTimeImmutable('2026-06-01 15:00', new DateTimeZone('Europe/Berlin')); echo 'New York 09:00 vs Berlin 15:00: ', $x->diff($y)->format('%h hours'), "\n"; // Counting dates, not 24-hour periods: set both times to midnight $m1 = new DateTimeImmutable('2026-03-01 23:30', $ny); $m2 = new DateTimeImmutable('2026-03-10 00:15', $ny); echo 'full days: ', $m1->diff($m2)->days, "\n"; echo 'calendar dates: ', $m1->setTime(0, 0)->diff($m2->setTime(0, 0))->days, "\n";
Output
Across the change, diff() reports 1 day, 0 hours because the clock reads 12:00 on both days, but only 23 hours went by. That is why the old (strtotime($b) - strtotime($a)) / 86400 recipe gives 0.9583, and 0 after intdiv, when the server runs in a DST zone. In UTC the same times are 24 hours apart. New York 09:00 and Berlin 15:00 in June are the same instant, so the difference is 0 hours. The last two lines show the other choice you have to make: from 23:30 on 1 March to 00:15 on 10 March is 8 full days but 9 calendar dates apart.
4Comparing two dates
DateTime and DateTimeImmutable objects work with the normal comparison operators: <, >, <=, >= and == compare the moments in time, even across time zones, and the spaceship operator <=> returns -1, 0 or 1, which is what usort wants. You don't need diff() just to find out which date comes first.
<?php function show(string $label, bool $value): void { echo $label, ': ', $value ? 'true' : 'false', "\n"; } $due = new DateTimeImmutable('2026-05-01 12:00'); $paid = new DateTimeImmutable('2026-04-28 09:15'); // <, >, <= and >= compare the moments in time show('paid < due', $paid < $due); show('paid > due', $paid > $due); // == compares the moment, === asks if it is the same object $same = new DateTimeImmutable('2026-05-01 12:00'); show('due == same', $due == $same); show('due === same', $due === $same); // The same instant in two zones is equal $utc = new DateTimeImmutable('2026-05-01 10:00', new DateTimeZone('UTC')); $berlin = new DateTimeImmutable('2026-05-01 12:00', new DateTimeZone('Europe/Berlin')); show('10:00 UTC == 12:00 Berlin', $utc == $berlin); // Same day? Compare the dates, because == includes the time show('due == 2026-05-01', $due == new DateTimeImmutable('2026-05-01')); show('same day', $due->format('Y-m-d') === '2026-05-01'); // <=> returns -1, 0 or 1, so it sorts dates directly $dates = [ new DateTimeImmutable('2026-07-04'), new DateTimeImmutable('2025-12-31'), new DateTimeImmutable('2026-01-15'), ]; usort($dates, fn($a, $b) => $a <=> $b); foreach ($dates as $d) { echo $d->format('Y-m-d'), "\n"; } // Date strings compare as text: fine for Y-m-d, wrong for m/d/Y show("'2026-05-01' < '2026-12-01'", '2026-05-01' < '2026-12-01'); show("'05/01/2026' < '12/01/2025'", '05/01/2026' < '12/01/2025');
Output
paid < due: true and due == same: true, but due === same: false, because === on objects checks identity. Ten in the morning in UTC and noon in Berlin compare equal because they are the same instant. == includes the time, so due == 2026-05-01 is false (midnight against noon); compare format('Y-m-d') strings for a same-day check. The last line is the trap with strings: '05/01/2026' < '12/01/2025' prints true because the text is compared character by character. Parse such dates into objects first.
5Business days between two dates
PHP has no built-in working-days function, but DatePeriod walks from one date to another in steps of a DateInterval, here one day (P1D). Skip Saturdays and Sundays with format('N'), the ISO day number where Monday is 1 and Sunday is 7, and skip any holidays you pass in. By default DatePeriod leaves out the end date; the DatePeriod::INCLUDE_END_DATE option (PHP 8.2) includes it. On older PHP, pass $end->modify('+1 day') as the end instead.
<?php // Counts Monday to Friday from $start to $end, both included function businessDays( DateTimeImmutable $start, DateTimeImmutable $end, array $holidays = [], ): int { $period = new DatePeriod( $start, new DateInterval('P1D'), $end, DatePeriod::INCLUDE_END_DATE, ); $count = 0; foreach ($period as $day) { // N is the ISO day of the week: 1 = Monday, 7 = Sunday if ((int) $day->format('N') >= 6) { continue; } if (in_array($day->format('Y-m-d'), $holidays, true)) { continue; } $count++; } return $count; } $start = new DateTimeImmutable('2026-12-01'); $end = new DateTimeImmutable('2026-12-31'); echo $start->format('l'), ' to ', $end->format('l'), "\n"; echo 'calendar days: ', $start->diff($end)->days, "\n"; echo 'business days: ', businessDays($start, $end), "\n"; echo 'without Christmas: ', businessDays($start, $end, ['2026-12-25']), "\n"; // Monday to Sunday of one week echo 'one week: ', businessDays( new DateTimeImmutable('2026-12-07'), new DateTimeImmutable('2026-12-13'), ), "\n"; // Without the option, DatePeriod stops before the end date $p = new DatePeriod($start, new DateInterval('P1D'), $end); echo 'dates without INCLUDE_END_DATE: ', iterator_count($p), "\n";
Output
December 2026 runs from a Tuesday to a Thursday. diff() says 30 calendar days between the 1st and the 31st, while the loop counts 23 business days with both ends included, and 22 once 25 December is a holiday. A full Monday-to-Sunday week gives 5. The last line shows the default: the period yields 30 dates and leaves out the 31st, so decide whether your end date counts and say so in the function name or a comment. The loop visits every day, which is fine for ranges of a few years.
6Age in years from a birth date
An age is the y field of the difference between the birth date and today. diff() already knows whether this year's birthday has passed, which a division of days by 365 gets wrong. In real code, use new DateTimeImmutable('today') for today's date at midnight; the example fixes "today" to 1 December 2026 so its output never changes.
<?php function age(DateTimeImmutable $birth, DateTimeImmutable $today): int { return $birth->diff($today)->y; } // A fixed "today" keeps the output stable; // in real code use new DateTimeImmutable('today') $today = new DateTimeImmutable('2026-12-01'); foreach (['1990-12-01', '1990-12-02', '2008-06-15'] as $date) { echo $date, ': ', age(new DateTimeImmutable($date), $today), "\n"; } // Years, months and days $birth = new DateTimeImmutable('2000-02-29'); echo $birth->diff($today)->format('%y years, %m months, %d days'), "\n"; // A 29 February birthday in a year without one echo '2027-02-28: ', age($birth, new DateTimeImmutable('2027-02-28')), "\n"; echo '2027-03-01: ', age($birth, new DateTimeImmutable('2027-03-01')), "\n"; // Days divided by 365 is off by one around the birthday $b = new DateTimeImmutable('1990-12-02'); echo 'days / 365: ', intdiv($b->diff($today)->days, 365), "\n";
Output
Someone born on 1 December 1990 is 36 on 1 December 2026, and someone born a day later is still 35. The leap-day birth date gives 26 years, 9 months, 2 days, and in 2027, which has no 29 February, diff() counts the birthday as 1 March: 26 on 28 February, 27 on 1 March. Some countries count 28 February instead, so check the rule if it matters legally. The last line shows why not to divide: intdiv of the days by 365 says 36 for the person who is still 35.
7Which should you use?
| Method | Gives you | Across DST | Best for |
|---|---|---|---|
| $a->diff($b) | y, m, d, h, i, s parts | Calendar time | "2 years, 2 months, 5 days", ages |
| $a->diff($b)->days | Total whole days | Calendar days | Days between two dates |
| $b->getTimestamp() - $a->getTimestamp() | Elapsed seconds | Real elapsed time | Total minutes or hours, durations |
| $a < $b, $a <=> $b | Order only | Compares instants | Which is first, sorting, deadlines |
| DatePeriod loop | Each day in a range | Calendar days | Business days, holidays |
| (strtotime($b) - strtotime($a)) / 86400 | Days as a float | Off by an hour | Avoid for days: use ->days |
Frequently asked questions
How do I get the number of days between two dates in PHP?
Create two DateTimeImmutable objects and read ->days from their difference: (new DateTimeImmutable('2026-01-01'))->diff(new DateTimeImmutable('2026-12-25'))->days is 358. It is always positive; check ->invert or use format('%R%a') when the order matters. Don't use ->d, which is only the days part after whole months.
How do I get the time difference in minutes in PHP?
Subtract the timestamps and divide by 60: intdiv($end->getTimestamp() - $start->getTimestamp(), 60). The i property of a DateInterval is only the minutes part (0 to 59), not the total, so $interval->i is wrong for any gap of an hour or more. For hours, divide by 3600.
Why is $interval->days false?
Only an interval returned by diff() knows its total days. An interval you build yourself, such as new DateInterval('P1M2D'), has days set to false, and format('%a') prints (unknown), because a month has no fixed length until it is applied to a date.
Why does diff() say 0 months and 29 days from 31 January to 1 March?
Months have different lengths, so month counts depend on where you start. In 2026, 2026-01-31 to 2026-03-01 gives 0 months, 29 days, while the same dates in the other order give 1 month, 1 day. If you need an exact count, use ->days (29 here) rather than the month and day parts.
Should I use DateTime or DateTimeImmutable?
Prefer DateTimeImmutable. Both have diff(), but DateTime::modify() changes the object in place and returns the same object, so $b = $a->modify('+10 days') moves $a too and $a->diff($b)->days is 0. With DateTimeImmutable, modify() returns a new object and leaves the original alone.
Why is my day count off by one when the clocks change?
Dividing a difference in seconds by 86400 assumes every day has 24 hours. In a zone with daylight saving time, the day the clocks move forward has 23 hours, so in America/New_York the gap from 7 to 8 March 2026 is 0.9583 days and intdiv makes it 0. Use diff()->days, which counts calendar days, and create the dates with an explicit DateTimeZone.
How do I calculate the difference between two dates on old PHP versions?
DateTime::diff() and date_diff() have been in PHP since 5.3, so they work on any host you are likely to meet; DateTimeImmutable arrived in 5.5. Only DatePeriod::INCLUDE_END_DATE is newer (PHP 8.2); on older versions add one day to the end date instead.