If the dates are in the same format than we will use a comparison operator. Let's see below example
<?php
$date_1 = "2023-10-01";
$date_2 = "2023-10-22";
if ($date_1 < $date_2) {
echo "$date_1 is less than $date_2";
else{
echo "$date_1 is greater than $date_2";
}
?>
Output: 2023-10-01 is less than 2023-10-22
If both dates are in different formats than we will use strtotime() function to convert the given dates into the corresponding timestamp format. After that we will compare using the comparison operator.
<?php
$date_1 = "2023-10-01";
$date_2 = "2023-10-22";
$timestamp_1 = strtotime($date_1);
$timestamp_2 = strtotime($date_2);
if ($timestamp_1 > $timestamp_2){
echo "$date_1 is greater than $date_2";
} else {
echo "$date_1 is less than $date_2";
}
?>
Output: 2023-10-01 is less than 2023-10-22