How To Compare Dates In PHP (Simple Examples)

Welcome to a quick tutorial on how to compare dates PHP. Need to find out which is an earlier or later date in PHP? Do some date comparisons?

The common ways to compare dates in PHP are:

  1. Parse the dates into date-time objects and compare them.
    • $dtA = new DateTime("2019-08-07");
    • $dtB = new DateTime("2017-08-09");
    • echo $dtA > $dtB ? "A is later" : "A is equal or earlier" ;
  2. Parse the dates into Unix timestamps and compare them.
    • $dtA = strtotime("2019-08-07");
    • $dtB = strtotime("2017-08-09");
    • echo $dtA > $dtB ? "A is later" : "A is equal or earlier" ;

Well, that covers the basics, but let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Date Compare Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

DATE COMPARISON

All right, let us now get into the examples of how to compare dates in PHP.

 

EXAMPLE 1) COMPARISON WITH DATE-TIME OBJECT

1-date-time.php
<?php
// (A) DATE TIME OBJECT
$dtA = new DateTime("2019-08-07");
$dtB = new DateTime("2017-08-09");
 
// (B) CAN BE COMPARED USING OPERATORS
if ($dtA > $dtB) { echo "A is later"; }
else if ($dtA < $dtB) { echo "B is later"; }
else { echo "AB are the same"; }

As in the introduction, this is probably one of the simplest common methods – Just parse the dates into new DateTime() objects, and use the “regular” comparison operators to compare both of them.

 

 

EXAMPLE 2) COMPARISON WITH UNIX TIMESTAMP

2-unix.php
<?php
// (A) UNIX TIMESTAMP
$dtA = strtotime("2019-08-07");
$dtB = strtotime("2017-08-09");

// (B) CAN BE COMPARED USING OPERATORS
if ($dtA > $dtB) { echo "A is later"; }
else if ($dtA < $dtB) { echo "B is later"; }
else { echo "AB are the same"; }

For you guys who are new, new DateTime() and strtotime() are 2 completely different mechanisms. DateTime is an object, while strtotime returns the Unix timestamp – The number of seconds that have elapsed since 1 Jan 1970 UTC; To make it simple, we are comparing objects with DateTime and integers with strtotime.

 

 

EXAMPLE 3) DATE STRING COMPARISON

3-string.php
<?php
// (A) WORKS IF DATE IS IN YYYY-MM-DD
$dtA = "2019-08-07";
$dtB = "2019-10-11";
 
if ($dtA > $dtB) { echo "A is later"; }
else if ($dtA < $dtB) { echo "B is later"; }
else { echo "AB are the same"; }

This is a “convenient” method that I spotted somewhere on the Internet… Just directly compare the date strings without parsing them into objects or integers. Yep, it works, but only if the date is in the format of YYYY-MM-DD. Try changing the date format and see the results for yourself:

3-string.php
<?php
// (B) THIS WILL NOT WORK
$dtA = "22 APR 1990";
$dtB = "22 JAN 1990";
 
if ($dtA > $dtB) { echo "A is later"; }
else if ($dtA < $dtB) { echo "B is later"; }
else { echo "AB are the same"; }

Yep – 22 Jan is later than 22 Apr… This is why I don’t recommend comparing date strings directly.

 

EXAMPLE 4) COMPARING MULTIPLE DATES

4-multiple.php
<?php
// (A) ARRAY OF DATES
$dates = ["2013-04-05", "2020-04-05", "2017-06-05", "2019-08-07"];
 
// (B) UNIX TIMESTAMP
$unix = [];
foreach ($dates as $d) { $unix[] = strtotime($d); }
 
// (C) SORT HIGH TO LOW
arsort($unix);
foreach ($unix as $k=>$u) {
  echo "$k - " . date("Y-m-d", $u)."<br>";
}

So what if we have multiple dates (more than 2) to compare? A simple solution will be to put the all into an array and sort them in order of high-to-low.

 

 

USEFUL BITS & LINKS

That’s all for this project, and here is a small section on some extras that may be useful to you.

 

SUMMARY – ALL THE COMMON DATE/TIME FUNCTIONS

Function Description Reference
$DATETIME = new DateTime(STRING) Create a new date-time object. Takes current server time by default, or provide a date/time string. Click Here
strtotime(STRING) Gets the Unix timestamp from the given date string. Click Here

 

INFOGRAPHIC CHEAT SHEET

Compare Dates In PHP (click to Enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to understand better, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.