In what situations might PHP output unexpected results when dealing with date calculations, and how can these issues be resolved?

When dealing with date calculations in PHP, unexpected results can occur when timezones are not properly set or when daylight saving time changes are not accounted for. To resolve these issues, it is important to always set the timezone explicitly and use PHP's date and time functions carefully.

// Set the timezone explicitly to avoid unexpected results
date_default_timezone_set('UTC');

// Perform date calculations with proper timezone handling
$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2022-02-01');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');