Finding Dates between Two Specified Date Ranges in PHP
To determine the dates that fall between two specified dates, such as April 20-April 22, 2010, several approaches can be employed in PHP.
Loop with Timestamps
This method utilizes timestamps to manage time intervals. The following code snippet demonstrates this approach:
<code class="php">$start = strtotime('20-04-2010 10:00'); $end = strtotime('22-04-2010 10:00'); for ($current = $start; $current <= $end; $current += 86400) { echo date('d-m-Y', $current); }
Loop with Date Increments
Another way is to use date increments to iterate over the day interval. Here's how it can be implemented:
<code class="php">for ($i = 0; $i <= 2; $i++) { echo date('d-m-Y', strtotime("20-04-2010 +$i days")); }
DatePeriod Class (PHP 5.3 )$
PHP 5.3 introduces the DatePeriod class, which simplifies the task of generating date ranges. Here's an example:
<code class="php">$period = new DatePeriod( new DateTime('20-04-2010'), DateInterval::createFromDateString('+1 day'), new DateTime('23-04-2010') // or pass in just the no of days: 2 ); foreach ( $period as $dt ) { echo $dt->format( 'd-m-Y' ); }</code>
The above is the detailed content of How to Find Dates between Two Specified Date Ranges in PHP?. For more information, please follow other related articles on the PHP Chinese website!