0

I am trying to get all dates between two dates using DatePeriod class. Its working fine when the dates inputed are of the same month, but not returning all dates when the two dates are of different months.

If the dates are say 2013-06-27 and 2013-07-05 its only returning 2013-06-27, 2013-06-28, 2013-06-29, 2013-06-30. Its not giving the rest of the dates.

CODE

 $begin = new DateTime($start);
 $last = new DateTime($end);
 $interval = DateInterval::createFromDateString('1 day');
 $period = new DatePeriod($begin, $interval, $last); 
Avinash
  • 1,935
  • 7
  • 29
  • 55

1 Answers1

1

I cannot reproduce the behavior

<?php
$start = '2013-06-27';
$end = '2013-07-05';

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

echo 'phpversion: ', phpversion(), "\n";

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

prints

phpversion: 5.4.7
Friday 2013-06-28 00:00:00
Saturday 2013-06-29 00:00:00
Sunday 2013-06-30 00:00:00
Monday 2013-07-01 00:00:00
Tuesday 2013-07-02 00:00:00
Wednesday 2013-07-03 00:00:00
Thursday 2013-07-04 00:00:00
done.

Which version of php do you use?

VolkerK
  • 95,432
  • 20
  • 163
  • 226