PHP
Date Difference
Programming
Web Development
Coding Tutorial

How to calculate the difference between two dates using PHP?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Calculating the difference between two dates in PHP is best done with DateTime or DateTimeImmutable and the diff() method. That approach is more reliable than manual timestamp subtraction when you care about calendar units such as years, months, and days.

Use DateTimeImmutable::diff() for Most Cases

The most robust modern solution is to create two date objects and ask PHP for the interval between them.

php
1<?php
2
3$start = new DateTimeImmutable('2024-01-01');
4$end = new DateTimeImmutable('2024-12-31');
5$interval = $start->diff($end);
6
7echo $interval->days . " days\n";
8echo $interval->y . " years, " . $interval->m . " months, " . $interval->d . " days\n";

This gives you a DateInterval object. That object exposes several useful fields:

  • 'y for years'
  • 'm for months'
  • 'd for days inside the month-based interval'
  • 'days for total day count'
  • 'invert to indicate direction'

That makes diff() the right answer when you want more than just raw seconds.

Total Days Versus Calendar Components

A common point of confusion is the difference between days and the y, m, d parts.

php
1<?php
2
3$start = new DateTimeImmutable('2024-01-15');
4$end = new DateTimeImmutable('2024-03-01');
5$interval = $start->diff($end);
6
7echo $interval->days . "\n";
8echo $interval->m . " months and " . $interval->d . " days\n";

$interval->days is the total number of days between the dates. The m and d properties describe the interval in calendar terms. Those are related but not the same thing.

If you need a billing or reporting answer in total days, use days. If you need a human-readable calendar-style difference, use the component fields.

Format the Interval for Display

PHP also lets you format a DateInterval for display.

php
1<?php
2
3$start = new DateTimeImmutable('2024-01-01');
4$end = new DateTimeImmutable('2025-03-10');
5$interval = $start->diff($end);
6
7echo $interval->format('%y years, %m months, %d days');

This is handy when you want a user-facing string and do not want to concatenate every piece manually.

Absolute Difference and Date Order

By default, diff() preserves direction. That means the interval can indicate that the first date is later than the second one.

php
1<?php
2
3$first = new DateTimeImmutable('2025-01-10');
4$second = new DateTimeImmutable('2024-12-31');
5$interval = $first->diff($second);
6
7echo $interval->invert; // 1 means negative direction

If you only care about the absolute difference, pass true as the second argument:

php
1<?php
2
3$first = new DateTimeImmutable('2025-01-10');
4$second = new DateTimeImmutable('2024-12-31');
5$interval = $first->diff($second, true);
6
7echo $interval->days;

That is useful when the answer should always be positive regardless of input order.

When Timestamp Math Is Acceptable

If you only want a rough difference in seconds, hours, or whole days and do not care about calendar semantics, timestamps can be enough.

php
1<?php
2
3$start = strtotime('2024-01-01 00:00:00');
4$end = strtotime('2024-01-03 12:00:00');
5$seconds = $end - $start;
6$hours = $seconds / 3600;
7
8echo $hours;

This is simple, but it is not the best tool when you want answers in months or years. Calendar math is more subtle than dividing seconds by a fixed constant.

Time Zones Matter

If the two dates are in different time zones, say so explicitly. Otherwise the result may be surprising.

php
1<?php
2
3$start = new DateTimeImmutable('2024-01-01 09:00:00', new DateTimeZone('America/Toronto'));
4$end = new DateTimeImmutable('2024-01-01 09:00:00', new DateTimeZone('Europe/London'));
5$interval = $start->diff($end);
6
7echo $interval->format('%h hours');

The two local times may look identical, but they do not refer to the same instant. DateTimeImmutable handles that correctly as long as the time zone information is present.

Why DateTimeImmutable Is a Good Default

DateTime and DateTimeImmutable both work, but DateTimeImmutable is often the safer default in application code because operations return new objects instead of mutating the original instance. That reduces accidental state changes in larger codebases.

For simple scripts, either class is fine. For reusable logic, immutability often makes date handling easier to reason about.

Common Pitfalls

One common mistake is subtracting timestamps and then trying to interpret the result as months or years. Calendar units do not map cleanly to fixed numbers of seconds.

Another pitfall is using the y, m, and d fields when the real requirement is total days. In that case, days is usually the property you want.

A third issue is ignoring time zones. Two strings that look similar can still represent different instants.

Finally, avoid mutating shared DateTime objects accidentally when DateTimeImmutable would keep the logic safer.

Summary

  • Use DateTimeImmutable::diff() for most PHP date-difference calculations.
  • Read days for total day count and y, m, d for calendar-style components.
  • Use format() to turn a DateInterval into a readable string.
  • Use absolute mode or inspect invert when date order may vary.
  • Prefer date objects over raw timestamp subtraction when calendar correctness matters.

Course illustration
Course illustration

All Rights Reserved.