udaya udaya - 2 months ago 7
PHP Question

How to find the dates between two specified date?

If I have two dates 20-4-2010 and 22-4-2010
in two text box and
I want the dates to be like this 20, 21, 22. How do I get that ?

Answer

I am pretty sure this has been answered a quadrillion times before, but anyway:

$start = strtotime('20-04-2010 10:00');
$end   = strtotime('22-04-2010 10:00');
for($current = $start; $current <= $end; $current += 86400) {
    echo date('d-m-Y', $current);
}

The 10:00 part is to prevent the code to skip or repeat a day due to daylight saving time.

By giving the number of days:

for($i = 0; $i <= 2; $i++) {
    echo date('d-m-Y', strtotime("20-04-2010 +$i days"));
}

With PHP5.3

$period = new DatePeriod(
    new DateTime('20-04-2010'),
    DateInterval::createFromDateString('+1 day'),
    new DateTime('23-04-2010') // or pass in just the no of days: 2
);

foreach ( $period as $dt ) {
  echo $dt->format( 'd-m-Y' );
}