To compare two dates in PHP, the code is as follows −
Example
<?php $date_1 = new DateTime("2020-11-22"); $date_2 = new DateTime("2011-11-22"); if ($date_1 > $date_2) echo $date_1->format("Y-m-d") . " is later than ". $date_2->format("Y-m-d"); else echo $date_1->format("Y-m-d") . " is before " . $date_2->format("Y-m-d"); ?>
Output
2020-11-22 is later than 2011-11-22
Two dates of ‘DateTime’ format are generated and checked to see which one is sooner or later. If the first date is later, relevant message is printed on the console. Otherwise, a message indicating that the first date is sooner than the second date is printed on the console.