How can a PHP script be optimized to loop through dates from Sunday to Saturday for a year, considering daylight saving time changes?

When looping through dates from Sunday to Saturday for a year in PHP, it's important to consider daylight saving time changes to ensure accurate date calculations. One way to optimize the script is to set the timezone to a specific region that observes daylight saving time, such as 'America/New_York', and use the DateTime class to iterate through the dates. By using the DateTime class and adjusting for daylight saving time changes, the script can accurately loop through dates for the entire year.

<?php

// Set the timezone to a region that observes daylight saving time
date_default_timezone_set('America/New_York');

// Initialize start date and end date for the loop
$startDate = new DateTime('first Sunday of January ' . date('Y'));
$endDate = new DateTime('last Saturday of December ' . date('Y'));

// Loop through dates from Sunday to Saturday for a year
$currentDate = $startDate;
while ($currentDate <= $endDate) {
    echo $currentDate->format('Y-m-d (l)') . "\n";
    $currentDate->modify('+1 week');
}

?>