Monday, June 27, 2016

Get / display all dates between two dates-php

It's very common uses to get all dates between two dates rang.

However php has a own class callled "DatePeriod" class which made our work easy for this task.


 $begin = new DateTime('2016-06-01');  
 $end = new DateTime('2016-06-28');  
 $end = $end->modify( '+1 day' );   
 $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);  
 foreach($daterange as $date){  
   echo $date->format("Y-m-d") . "<br>";  
 }  

Note :
 //(P1D stands for period of one day, see DateInterval for further documentation)
 //$end = $end->modify( '+1 day' ); mean dates include the last day 2016-06-28  

Output will looks like
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-05

Total Pageviews