How to Compare Two Dates in PHP?

Problem:

You want to compare two dates and find out which one is more recent(greater).

Solution:

There are multiple ways to compare two dates. In the following, you’ll find 2 methods to compare two dates.

Method 1: Using strtotime() function

In this way, we’ll compare two dates by two steps-

  1. Convert two dates into their respective timestamps. The timestamp of a date, as you know, is number of seconds since January 1 1970 00:00:00 UTC.
  2. Then, find out the greater date comparing those two timestamps.

The following code show how to use strtotime() function to compare two dates-

<?php
$date1 = "2014-05-26";
$date2 = "2014-05-17";
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
if($timestamp1 == $timestamp2){
    echo "$date1 is equal to the $date2";
}else{
    echo ($timestamp1>$timestamp2)? "$date1 is greater than the $date2": "$date2 is greater than the $date1";
}
?>

[wpdm_file id=117]

Output:
2014-05-26 is greater than the 2014-05-17

How it works:

Line 2 -3 $date1 holds the first date and line 2 holds the second date.
Line 4 Using strtotime() function, we convert the first date to its equivalent timestamp.
Line 5 Using strtotime() function, we convert the second date to its equivalent timestamp.
Line 6-7 In line 6, we compare if the two dates are equal. If so, then, we print it in line 7
Line 8-11 If two dates are not equal, then thses lines execute
Line 9 The ternary operator checks if $timestamp1(which is the first date) is greater than $timestamp2. If so(which is actually does), it prints the string before the colon(:), if not, it prints the next string after colon.

Other than the yyyy-mm-dd, you can use other date format in the strtotime() function. To know which date format strtotime() function supports, please check supported date and time formats.

Method 2: Using diff() method of DateTime class from SPL

One of the classes that SPL(Standard PHP Library) supplies is DateTime class and it has a method named diff() which can returns difference between two date objects. We’ll apply the following steps to compare the two dates using this method-

  1. Convert each date to its Date equivalent DateTime object using DateTime class.
  2. Calculate the day interval between two dates as object using diff() method.
  3. Convert the Day interval object to its equivalent day number using format() method which will reveal which date is greater than the other.

See the following example to see how the method works-

<?php
$date1 = "2014-05-26";
$date2 = "2014-05-17";
$datetimeObj1 = new DateTime($date1);
$datetimeObj2 = new DateTime($date2);
$interval = $datetimeObj1->diff($datetimeObj2);
$dateDiff = $interval->format('%R%a');

if($dateDiff == 0){
echo "$date1 is equal to the $date2";
}else{
echo ($dateDiff > 0)? "$date2 is greater than the $date1": "$date1 is greater than the $date2";
}
?>

[wpdm_file id=118]

Output:
2014-05-26 is greater than the 2014-05-17 

How it works:

Line 2-3 $date1 holds the first date and line 2 holds the second date.
Line 4-5 Using the two dates($date1 and $date2), we create two DateTime objects ($datetimeObj1, $datetimeObj2) from the DateTime class.
Line 6 The diff() method will substract from $datetimeObj2 to $datetimeObj1 and return it as an object($interval).
Line 7 Here, we use format() method to convert the $interval object to human readable format. %R displays the sign of the difference between two dates and %a displays the total number of days. If you prints the variable $dateDiff, it will display -9.
Line 9-10 If two dates are same, the text in line 10 will display.
Line 11-13 If two dates are not equal these lines will be executed.
Line 12 As $dateDiff is -9, so the string after colon(:) will be displayed as output.