Difference Between Dates in PHP: 5 Approaches from Simple to Professional

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Calculate the Difference Between Two Dates in PHP: From Beginner to Pro

\n\n

Have you ever wondered: "How many days have passed since my last vacation?" or "How many hours until the promotion on the site ends?"? If you are a PHP developer, these questions turn into one task: how to calculate the difference between two dates.

\n\n

This question is one of the most popular on Stack Overflow with thousands of views. And for good reason: working with dates in PHP can be tricky. Leap years, time zones, different formats — all of this can break even the most well-thought-out code.

\n\n

In this article, we will cover 5 proven ways to calculate the difference between dates in PHP — from the simplest to professional ones. You will learn when to use which method and get ready-made code examples.

\n\n

1. Classic Method: strtotime() and Simple Arithmetic

\n\n

The oldest and most intuitive way is to convert dates to Unix timestamps using the strtotime() function and simply subtract one number from another.

\n\n
<?php\n$date1 = strtotime('2024-01-15');\n$date2 = strtotime('2024-03-20');\n\n$diff_seconds = $date2 - $date1;\n$diff_days = floor($diff_seconds / (60 * 60 * 24));\n\necho "Difference in days: " . $diff_days; // Outputs: 65\n?>
\n\n

Pros: simplicity, works in any PHP version.

\n

Cons: does not account for time zones, issues with dates before 1970, no built-in support for months and years (since their length varies).

\n\n

This method is suitable for quick "back-of-the-envelope" calculations, but for serious projects, it is better to use more modern tools.

\n\n

2. Modern Standard: DateTime Class and diff() Method

\n\n

Starting from PHP 5.2, we have the powerful DateTime class. This is a true Swiss Army knife for working with dates. The diff() method returns a DateInterval object, which contains all the information about the difference.

\n\n
<?php\n$date1 = new DateTime('2024-01-15');\n$date2 = new DateTime('2024-03-20');\n\n$interval = $date1->diff($date2);\n\necho "Difference: " . $interval->days . " days"; // 65\necho "or " . $interval->m . " months and " . $interval->d . " days"; // 2 months and 5 days\n?>
\n\n

DateInterval provides access to the following properties:

\n
    \n
  • y — years
  • \n
  • m — months
  • \n
  • d — days
  • \n
  • h — hours
  • \n
  • i — minutes
  • \n
  • s — seconds
  • \n
  • days — total number of days (the most useful!)
  • \n
  • invert — flag: 0 if the first date is earlier, 1 if later
  • \n
\n\n

This method accounts for leap years and works correctly with various calendar peculiarities. This is the method recommended in the official PHP documentation.

\n\n

3. Formatting the Difference: How to Display "2 years 3 months 10 days"

\n\n

Often you need not just to get a number, but to nicely format the difference for the user. The format() method of the DateInterval object comes to the rescue.

\n\n
<?php\n$birthday = new DateTime('1990-05-15');\n$today = new DateTime();\n\n$age = $birthday->diff($today);\n\necho $age->format('%y years %m months %d days');\n// Example: 34 years 2 months 3 days\n?>
\n\n

Format specifiers:

\n
    \n
  • %y — years
  • \n
  • %m — months
  • \n
  • %d — days
  • \n
  • %h — hours
  • \n
  • %i — minutes
  • \n
  • %s — seconds
  • \n
  • %a — total number of days (alternative to days)
  • \n
  • %r — minus sign if the difference is negative
  • \n
\n\n

Want to inflect the words "day/days"? You will have to write your own log

Blogs

Book Recommendations