How to compare two DateTime objects in PHP?

Spread the love

Sometimes, we want to compare two DateTime objects in PHP.

In this article, we’ll look at how to compare two DateTime objects in PHP.

How to compare two DateTime objects in PHP?

To compare two DateTime objects in PHP, we can use comparison operators.

For instance, we write

$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 === $date2);
var_dump($date1 < $date2); 
var_dump($date1 > $date2);

to create 2 DateTime objects $date1 and $date2.

And then we compare them with === , < and >.

Concluion

To compare two DateTime objects in PHP, we can use comparison operators.

Leave a Reply

Your email address will not be published. Required fields are marked *