How can PHP DateTime (DateTimeImmutable) be used to compare date values effectively?

When comparing date values in PHP using DateTime or DateTimeImmutable objects, it is important to ensure that the dates are in the same format and timezone. One effective way to compare date values is by using the `diff()` method to calculate the difference between two dates. This method returns a DateInterval object that can be used to determine if one date is greater than, less than, or equal to another date.

$date1 = new DateTimeImmutable('2022-01-01');
$date2 = new DateTimeImmutable('2022-02-01');

$interval = $date1->diff($date2);

if ($interval->days > 0) {
    echo $date2->format('Y-m-d') . ' is greater than ' . $date1->format('Y-m-d');
} elseif ($interval->days < 0) {
    echo $date1->format('Y-m-d') . ' is greater than ' . $date2->format('Y-m-d');
} else {
    echo $date1->format('Y-m-d') . ' is equal to ' . $date2->format('Y-m-d');
}