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/>";
}

?>

Categories: web development php

Trending Courses

CodeIgniter

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Advance Digital Marketing

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 20 Days

Crash : 7 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 7 Month

Fastrack : 4 Month

Crash : 2 Month

Related Blogs

Request For Demo