What role does setting the default timezone play in PHP when working with DateTime objects, and how can it impact the functionality of the code?
Setting the default timezone in PHP is crucial when working with DateTime objects because it ensures that date and time calculations are accurate and consistent across different parts of your code. If the default timezone is not set, PHP will use the server's timezone by default, which can lead to unexpected results when working with dates and times. To avoid potential issues, it's recommended to set the default timezone explicitly to the timezone that matches your application's requirements.
// Set the default timezone to UTC
date_default_timezone_set('UTC');
// Now you can create DateTime objects with confidence
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');