Are there best practices for handling time calculations in PHP to avoid incorrect results?

When working with time calculations in PHP, it's important to use the correct functions and formats to avoid incorrect results. One common mistake is not considering time zones, which can lead to discrepancies in calculations. To ensure accurate time calculations, it's best to use PHP's built-in DateTime class along with methods like add() and diff() to manipulate and compare dates and times.

// Example of handling time calculations in PHP using DateTime class

// Create DateTime objects for start and end times
$start = new DateTime('2022-01-01 08:00:00', new DateTimeZone('UTC'));
$end = new DateTime('2022-01-01 12:30:00', new DateTimeZone('UTC'));

// Calculate time difference between start and end times
$interval = $start->diff($end);

// Output the difference in hours and minutes
echo "Time difference: " . $interval->format('%h hours %i minutes');