tags:

views:

90

answers:

4

How to implement the function ?

So it'll output an array containing 2009-12-25 2009-12-26 2009-12-27 2009-12-28 2009-12-29 2009-12-30?

A: 

Use mktime with an incrementing variable for the day:

$array = array();
for ($x = 25; $x <= 30; $x++) {
    $array[] = date('Y-m-d', mktime(0, 0, 0, 12, $x, 2009));
}
deceze
Of course this approach can't handle a month cross-over
therefromhere
True, but that wasn't asked for.
deceze
@therefromhere Actually, false, `mktime(0, 0, 0, 12, 32, 2009)` is January 1st 2010. If you're looking for a "start date + x days" algorithm this is about this simplest way to do it.
deceze
+5  A: 
$startdate = strtotime("2009-12-25");
$enddate = strtotime("2009-12-30");
$steps = "1 day"; 

# Start
$date = $startdate;
while ($date <= $enddate)  
  {   echo date("Y-m-d", $date)."<br>";
      $date = strtotime ("+".$steps, $date); // can be slow with large arrays, 
                                             // you can also just add 60*60*24 seconds
                                             // to $date

   }
Pekka
FYI if this array was very large, running strtotime() on every element in the array can be slow, so keep in mind in those situations you could always store the dates without the dashes, still using the YearMonthDay format, and you can run simple > and < on those numbers.
TravisO
True that. I added a comment.
Pekka
+3  A: 

Besides you can use approach similar to the first answer.

 $startDate = mktime(0, 0, 0, 12, 25, 2009);
 $endDate = mktime(0, 0, 0, 12, 30, 2009);
 for ($i=$startDate; $i<=$endDate; $i=$i+86400) {
   $timeArray[]=date("Y-m-d", $i);
 }

Result:

array (
  0 => '2009-12-25',
  1 => '2009-12-26',
  2 => '2009-12-27',
  3 => '2009-12-28',
  4 => '2009-12-29',
  5 => '2009-12-30',
)
Alex
+2  A: 

As of php 5.3 you can also use the DatePeriod class.

VolkerK