What potential pitfalls should be considered when using DateTime objects in PHP to calculate calendar dates?

One potential pitfall when using DateTime objects in PHP to calculate calendar dates is not taking into account time zones. It's important to always set the correct time zone when working with DateTime objects to ensure accurate calculations and comparisons. Failure to do so can lead to unexpected results, especially when dealing with daylight saving time changes.

// Set the default time zone to avoid unexpected results
date_default_timezone_set('America/New_York');

// Create a DateTime object with the correct time zone
$date = new DateTime('2022-12-31');

// Output the date in the correct time zone
echo $date->format('Y-m-d');