How to Compare Dates in PHP

Comparing dates becomes necessary when you are creating booking and event scheduling plugins or websites in PHP. It has many other uses as well, depending on how you implement things. For example, we might use it to create time-sensitive promotional events in our PHP scripts.

PHP comes with dedicated functions and methods that you can use to compare dates when writing procedural or object-oriented style code. In this tutorial, we will discuss them both.

Compare Dates in PHP (Object-Oriented Style)

There is a DateTime class in PHP that you can use to create DateTime objects. You can pass it a valid date/time string along with an optional timezone argument to create a DateTime object. Here are a few examples:

<?php

$past = new DateTime(“18 May 2021”);
$now = new DateTime(“now”);
$dist_past = new DateTime(“2002-09-21 18:15:00”);
$dist_future = new DateTime(“12-09-2036”);

?>

You can use simple comparison operators on these DateTime objects to compare the dates now and check whether the returned value is true or false.

<?php

$past = new DateTime(“18 May 2021”);
$now = new DateTime(“now”);
$dist_past = new DateTime(“2002-09-21 18:15:00”);
$dist_future = new DateTime(“12-09-2036”);

if($past < $now) {
echo ‘The first date is in the past.’;
}
// Output: The first date is in the past.

if($past > $dist_past) {
echo ‘The third date is in the distant past.’;
}
// Output: The third date is in the distant past.

if($dist_future > $now) {
echo ‘The fourth date is in the future.’;
}
// Output: The fourth date is in the future.

?>

Difference of Dates in PHP (Object-Oriented Style)

Sometimes, you might want to get some more information when comparing dates. There are dedicated methods and functions in PHP to calculate the difference between dates.

Once you have created your DateTime objects, you can also call the diff() method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object.

Keep in mind that the date on which you called diff() will be subtracted from the date passed to this method. You can format the difference any way you like after that by using the format() method. Here are some examples:

<?php

$last = new DateTime(“25 Dec 2020”);
$now = new DateTime(“now”);
$next = new DateTime(“25 Dec 2021”);

$days_last = $last->diff($now);

$days_next = $now->diff($next);

echo $days_last->format(‘%a days’).’ since last Christmas.’;
// Output: 170 days since last Christmas.

echo $days_next->format(‘%a days’).’ to next Christmas.’;
// Output: 194 days to next Christmas.

?>

You can pass many parameters to the format() method to control how PHP outputs the difference. In our example, we used %r to determine the relativity of the difference. A positive value means that the date passed to diff() is in the future in comparison to the date which called it. A negative value means that the date passed to diff() is in the past in comparison to the date which called it. The %a string is used to show the number of days and hours. You can also use other strings like %H, %I, and %S to show hours, minutes, and seconds respectively. All these format characters can be found in the documentation for the format() method.

Compare Dates in PHP (Procedural Style)

You can use a different set of functions if you prefer to write procedural style code. In this case, we can use the date_create() function to create our DateTime objects from the passed string. The variables you define will still store DateTime objects, but they won’t be explicitly instantiated by you.

Since we are still dealing with DateTime objects, the regular comparison operators will still allow us to compare these dates.

<?php

$past = date_create(“18 May 2021”);
$now = date_create(“now”);
$dist_past = date_create(“2002-09-21 18:15:00”);
$dist_future = date_create(“12-09-2036”);

if($past < $now) {
echo ‘The first date is in the past.’;
}
// Output: The first date is in the past.

if($past > $dist_past) {
echo ‘The third date is in the distant past.’;
}
// Output: The third date is in the distant past.

if($dist_future > $now) {
echo ‘The fourth date is in the future.’;
}
// Output: The fourth date is in the future.

?>

Difference of Dates in PHP (Procedural Style)

Just like the diff() method of the DateTime class, there is a corresponding date_create() function that you can use to calculate the difference between two dates. It accepts two parameters, and the date provided in the first parameter is subtracted from the date in the second parameter.

Here’s an example to calculate the number of days since last Christmas and the number of days to next Christmas.

<?php

$last = date_create(“25 Dec 2020”);
$now = date_create(“now”);
$next = date_create(“25 Dec 2021”);

$days_last = date_diff($last, $now);

$days_next = date_diff($now, $next);

echo date_interval_format($days_last, ‘%a days’).’ since last Christmas.’;
// Output: 170 days since last Christmas.

echo date_interval_format($days_next, ‘%a days’).’ to next Christmas.’;
// Output: 194 days to next Christmas.

?>

We wanted to keep the returned number of days positive in both cases, so the time that comes later is passed as the second parameter.

Final Thoughts

PHP comes with handy methods and functions to compare two dates, depending on your coding style. You can get more information like the exact time difference between two dates using the diff() method and the date_diff() function in PHP. I have tried to cover the basics of comparing dates in PHP in this tutorial. You should consider reading the documentation to learn how to format the difference in dates in more advanced ways.

Generated by Feedzy