What are the potential pitfalls when working with DateTime in PHP across time zone changes like daylight saving time?
When working with DateTime in PHP across time zone changes like daylight saving time, a potential pitfall is that the time may not be accurately represented if the time zone is not properly set. To solve this issue, it is important to always set the time zone explicitly when working with DateTime objects in PHP to ensure that the correct offset is applied.
// Set the default time zone to UTC
date_default_timezone_set('UTC');
// Create a new DateTime object with the correct time zone
$datetime = new DateTime('now', new DateTimeZone('America/New_York'));
// Output the current date and time with the correct time zone
echo $datetime->format('Y-m-d H:i:s');