Show A List Of Days Between Two Dates in PHP

0
1352

The following function will show you a list of days between two dates. The following code will require to setup a start date on variable $start and a end date on variable $end. The rest will do the code for you

// Required to set the default timezone to work with DateTime functions
date_default_timezone_set('America/Sao_Paulo');

$start = new DateTime('2010-10-01'); //start date
$end = new DateTime('2010-10-05'); //end date

$period = new DatePeriod(
  $start, // 1st PARAM: start date
  new DateInterval('P1D'), // 2nd PARAM: interval (1 day interval in this case)
  $end, // 3rd PARAM: end date
  DatePeriod::EXCLUDE_START_DATE // 4th PARAM (optional): self-explanatory
);


foreach($period as $date) {
  echo $date->format('Y-m-d').'<br/>'; // Display the dates in yyyy-mm-dd format
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here