Home >Backend Development >PHP Tutorial >How to Extract Date Ranges Between Two Dates in PHP?
Getting Dates Between Two Specified Dates in PHP
Given two dates in text boxes (e.g., "20-4-2010" and "22-4-2010"), the goal is to extract the dates between them and format them as "20, 21, 22."
PHP Solution:
To achieve this, utilize the following PHP code:
<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); }
Explanation:
Additional Options:
<code class="php">for ($i = 0; $i <= 2; $i++) { echo date('d-m-Y', strtotime("20-04-2010 +$i days")); }
<code class="php">$period = new DatePeriod( new DateTime('20-04-2010'), DateInterval::createFromDateString('+1 day'), new DateTime('23-04-2010') ); foreach ($period as $dt) { echo $dt->format('d-m-Y'); }</code>
The above is the detailed content of How to Extract Date Ranges Between Two Dates in PHP?. For more information, please follow other related articles on the PHP Chinese website!