What are some common pitfalls when calculating time intervals using date_diff in PHP?
One common pitfall when calculating time intervals using date_diff in PHP is not accounting for daylight saving time changes, which can lead to inaccurate results. To solve this issue, it is recommended to use the DateTime objects with timezones set explicitly.
$date1 = new DateTime('2022-03-15 10:00:00', new DateTimeZone('UTC'));
$date2 = new DateTime('2022-03-20 12:00:00', new DateTimeZone('UTC'));
$interval = $date1->diff($date2);
echo $interval->format('%R%a days %H hours %I minutes');