What are the best practices for iterating over a range of dates in PHP, such as for calculating weekly values?

When iterating over a range of dates in PHP for calculating weekly values, it is best to use the DateTime class to handle date manipulation and iteration efficiently. By setting a start date and end date, you can loop through the dates week by week using the DateInterval class to increment the date by 7 days each iteration.

// Set start and end dates
$start_date = new DateTime('2022-01-01');
$end_date = new DateTime('2022-12-31');

// Iterate over the range of dates week by week
while ($start_date <= $end_date) {
    $end_of_week = clone $start_date;
    $end_of_week->modify('+6 days');
    
    // Calculate weekly values here
    
    // Move to the next week
    $start_date->modify('+1 week');
}