What are some best practices for handling time calculations in PHP to ensure accuracy and consistency?

When handling time calculations in PHP, it is important to use the appropriate functions and methods to ensure accuracy and consistency. One common mistake is not considering time zones, which can lead to incorrect results. To address this issue, always set the correct time zone using `date_default_timezone_set()` and use functions like `strtotime()` or `DateTime` for accurate time calculations.

// Set the default time zone
date_default_timezone_set('America/New_York');

// Calculate the difference between two dates
$date1 = strtotime('2022-01-01 12:00:00');
$date2 = strtotime('2022-01-02 12:00:00');
$timeDiff = $date2 - $date1;

// Format the time difference
echo "Time difference: " . gmdate("H:i:s", $timeDiff);