3 Ways 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 – if (new DateTime("DATE A") > new DateTime("DATE B")) { ... }
  2. Parse the dates into Unix timestamps and compare them – if (strtotime("DATE A") > strtotime("DATE B")) { ... }
  3. Directly compare date strings that are in the format of “YYYY-MM-DD” – if ("DATE A" > "DATE B") { ... }

Well, that covers the quick basics. But let us walk through the detailed examples in this guide – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP 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.

 

 

EXTRAS

That’s all for this tutorial, 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 from the given STRING. Will take the current server time if STRING is not provided. Click Here
strtotime(STRING) Gets the Unix timestamp from the given date string. Click Here

 

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. Required fields are marked *