How to compare two dates in PHP (with DateTime)

If you want to compare two dates in PHP using DateTime, you can use math operators (>, <, ==)

An example:

$date1 = new DateTime('2022-09-26 16:12:20');
$date2 = new DateTime('2022-01-11 10:13:30');
var_dump($date1 == $date2); //false
var_dump($date1 > $date2); // true
var_dump($date1 < $date2); // false


Comments

No comments yet, you can leave yours: