How to Iterate loop Between two Dates
Sometimes, we need to iterate the loop between two dates, for example we have to update data or insert data in the database between two dates. In PHP, we can quickly iterate the loop between date ranges using the date function of PHP DateTime, date period, etc.
HTML CODE :
<p><label style="margin-bottom: 12px;" for="password">From</label></p>
<p><input id="dateInput" class="form-control" style="margin-bottom: 12px;" type="date" name="from" /></p>
<p><label style="margin-bottom: 12px;" for="password">To</label></p>
<p><input id="dateInput1" class="form-control" style="margin-bottom: 12px;" type="date" name="to" /></p>
PHP CODE :
<?php
$begin = new DateTime($_POST['from']);
$end = new DateTime($_POST['to']);
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date)
{
echo $date->format("Y-m-d") . "<br/>";
}
?>