What potential pitfalls should be considered when calculating and formatting time durations in PHP?

When calculating and formatting time durations in PHP, one potential pitfall to consider is handling time zones correctly. If your application deals with users in different time zones, you need to ensure that the calculations are done in the appropriate time zone to avoid discrepancies. Additionally, be mindful of daylight saving time changes which can affect the accuracy of time duration calculations.

// Set the default time zone to UTC
date_default_timezone_set('UTC');

// Calculate the time duration in the desired time zone
$startTime = new DateTime('2022-01-01 08:00:00', new DateTimeZone('America/New_York'));
$endTime = new DateTime('2022-01-01 12:30:00', new DateTimeZone('America/New_York'));
$duration = $startTime->diff($endTime);

// Format the time duration
echo $duration->format('%H hours %i minutes');