What are common errors to watch out for when comparing datetime variables in PHP using date_diff()?

When comparing datetime variables in PHP using date_diff(), common errors to watch out for include not using the correct format for the datetime variables, not handling timezone differences properly, and not considering daylight saving time adjustments. To avoid these errors, make sure to use the correct datetime format, set the correct timezone for each datetime variable, and account for any daylight saving time changes.

$date1 = new DateTime('2022-01-01 12:00:00', new DateTimeZone('UTC'));
$date2 = new DateTime('2022-01-02 12:00:00', new DateTimeZone('UTC'));

$interval = date_diff($date1, $date2);

echo $interval->format('%R%a days');