0

I want to get all the dates between the start date and end date. I need to show a weekly data, and the data is split into day wise.

I have a start date as 2015-11-02 and end date as 2015-11-08. Then I need to print all the dates in between in a foreach loop.

How can I do that?

nirvair
  • 4,001
  • 10
  • 51
  • 85

1 Answers1

0

One could use string manipulation and basic math, but months with different number of days and leap years etc make this a futile approach.

It is better to use an instance of PHP DateTime and loop through that, which is a question already answered here:

$begin = new DateTime( '2010-05-01' );
$end = new DateTime( '2010-05-10' );

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ( $period as $dt )
  echo $dt->format( "l Y-m-d H:i:s\n" );

The DateTime object has the added advantage of outputting various formats, useful when generating link names, URIs, SQL queries, etc.

Community
  • 1
  • 1
NoChecksum
  • 1,206
  • 1
  • 14
  • 31