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 Expert Course

Regular : 6 Months

Fastrack : 3 Months

Crash : 2 Months

React JS

Regular : 45 Days

Fastrack : 25 Days

Crash : 15 Days

Laravel

Regular : 45 Days

Fastrack : 20 Days

Crash : 10 Days

Front End Developer

Regular : 6 Months

Fastrack : 4 Months

Crash : 2 Months

Web Expert with Python

Regular : 12 Months

Fastrack : 6 Months

Crash : 3 Months

Related Blogs

Request For Demo