Home » Programming » PHP » Php Compare Dates

How to Compare Dates in PHP, With Examples

This tutorial will show you the best way to compare dates in the PHP programming language – and provide code examples.

It’s essential to be able to accurately compare dates when building your app. For example, you may wish to compare dates for reporting purposes – retrieving a list of transactions from a shop for a given reporting period, or you may want to have your user enter a fake birthday into your webpage so that they can pretend that they are over 18.

Anyway, here’s how to compare dates in PHP.

Comparing Dates in PHP The Hard Way

PHP has several built-in ways to compare dates, depending on how the dates you wish to compare are stored.

Comparing String Dates in PHP

The strtotime() function converts a string to a numerical value representing the number of milliseconds that have passed since 1 January 1970.

This makes it easy to compare any two date strings – so long as they are formatted in a way that strtotime() can understand.

// Convert some string dates to integer values using strtotime() for comparison
$date1 = strtotime('2021-11-19');
$date2 = strtotime('2020-08-17');

// Compare the dates
if ($date1 > $date2)
    echo "$date1 is later than $date2";
else
    echo "$date1 is earlier than $date2";

Note that strtotime() accepts dates in a variety of formats which can be a bit ambiguous (i.e., American date formatting vs. the rest of the world date formatting).

The DateTime Class – A Better Way

The PHP DateTime class is specifically designed for storing a date and time and can include timezone adjustments. It’s the best method for storing time/date information in vanilla PHP.

// Define some DateTime objects which can be directly compared
$date1 = new DateTime("2021-11-19");
$date2 = new DateTime("2020-08-17");

// Compare DateTime objects
if ($date1 > $date2)
    echo $date1->format("Y-m-d") . " is later than " . $date2->format("Y-m-d");
else
    echo $date1->format("Y-m-d") . " is earlier than " . $date2->format("Y-m-d");

Above, the DateTime class is used to represent two different date values which are then compared. The DateTime class also includes useful methods for formatting and displaying dates.

DateTime Differences

Suppose you want to know the difference between two dates. In that case, you can use the diff() method in the DateTime class to get a DateInterval class variable which contains information about the difference between two dates:

$date1 = new DateTime('2021-11-19');
$date2 = new DateTime('2020-08-17');
$interval = $date1->diff($date2);
echo $interval->format("%a:%h:%i:%s"); // Format to number of days:hours:minutes:seconds

Above, two DateTime objects are defined, and the interval is calculated using the diff() method of one of the DateTime objects. Like the DateTime class, the DateInterval class contains helper functions for formatting and displaying the time difference.

Comparing Dates in PHP The Smart Way

Carbon is a PHP library specializing in handling dates, times, timezones, time differences, and everything else date/time-related.

You can find out about it at:

https://carbon.nesbot.com/docs/

Where it is extensively documented.

Carbon makes dealing with dates and times in PHP a breeze. It removes a lot of the ambiguity in formatting and confusion with timezones, taking over most of the heavy lifting so you can get on with building your app, rather than trying to figure out the effect of daylight savings in Broken Hill, New South Wales, which is actually in the South Australia Timezone, in relation to your app which is hosted in Belfast.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment