How to Iterate Through a Range of Dates in PHP
In PHP, you may encounter the need to iterate through a series of dates within a specific range. To achieve this, you can leverage the DatePeriod class.
Consider the example where you have two dates, '2010-05-01' and '2010-05-10', and want to loop through all the dates in between.
$begin = new DateTime('2010-05-01'); $end = new DateTime('2010-05-10'); $interval = DateInterval::createFromDateString('1 day'); $period = new DatePeriod($begin, $interval, $end); foreach ($period as $dt) { echo $dt->format("l Y-m-d H:i:s\n"); }
Here's a breakdown of the code:
By leveraging the DatePeriod class, you can easily iterate through a range of dates and perform operations on each date within that range.
The above is the detailed content of How to Iterate Through a Date Range in PHP?. For more information, please follow other related articles on the PHP Chinese website!