What are common pitfalls when calculating time differences in PHP functions?

Common pitfalls when calculating time differences in PHP functions include not considering time zones, not accounting for daylight saving time changes, and not handling leap years properly. To avoid these issues, it's important to always work with datetime objects and use the appropriate functions to calculate time differences accurately.

// Example code snippet to calculate time difference accurately

$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 = $date1->diff($date2);

echo $interval->format('%d days %h hours %i minutes %s seconds');