How to Calculate the Difference Between Two Dates in PHP: From Beginner to Pro
\n\nHave 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\nThis 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\nIn 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\n1. Classic Method: strtotime() and Simple Arithmetic
\n\nThe oldest and most intuitive way is to convert dates to Unix timestamps using the strtotime() function and simply subtract one number from another.
<?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\nPros: simplicity, works in any PHP version.
\nCons: does not account for time zones, issues with dates before 1970, no built-in support for months and years (since their length varies).
\n\nThis method is suitable for quick "back-of-the-envelope" calculations, but for serious projects, it is better to use more modern tools.
\n\n2. Modern Standard: DateTime Class and diff() Method
\n\nStarting 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.
<?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\nDateInterval 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
This method accounts for leap years and works correctly with various calendar peculiarities. This is the method recommended in the official PHP documentation.
\n\n3. Formatting the Difference: How to Display "2 years 3 months 10 days"
\n\nOften 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.
<?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\nFormat 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 todays) \n %r— minus sign if the difference is negative \n
Want to inflect the words "day/days"? You will have to write your own log