What are the potential pitfalls of using for loops to iterate over date values in PHP?

Using for loops to iterate over date values in PHP can be problematic because it doesn't account for differences in month lengths, leap years, or daylight saving time changes. To properly iterate over date values, it's better to use the DateTime object and its methods for adding intervals.

// Using DateTime object to iterate over date values
$startDate = new DateTime('2022-01-01');
$endDate = new DateTime('2022-01-31');

$currentDate = clone $startDate;

while ($currentDate <= $endDate) {
    echo $currentDate->format('Y-m-d') . "\n";
    $currentDate->modify('+1 day');
}