Skip to content
PHP Logo

What are the best practices for incrementing dates in a loop in PHP?

When incrementing dates in a loop in PHP, it is important to ensure that the date format is consistent and that the incrementation is done correctly to avoid any unexpected results. One common approach is to use the `strtotime` function to increment the date by a specific interval, such as one day.

php
// Start date
$start_date = '2022-01-01';

// End date
$end_date = '2022-01-10';

// Loop through dates
$current_date = $start_date;
while (strtotime($current_date) <= strtotime($end_date)) {
    echo $current_date . "\n";
    $current_date = date('Y-m-d', strtotime($current_date . ' +1 day'));
}