06-29-2010, 10:43 PM
I would like to iterate through a range of dates--I'm thinking PHP's DateTime class will work best for this. Here is the question--if I have a start date and an end date, how can I iterate through every date between the two (inclusive)?
Did I state this clearly?
06-29-2010, 10:54 PM
What version of PHP are you using?
06-29-2010, 11:30 PM
Dang. 5.3.0 has a DatePeriod class thats built in to do just this task for you.
Prior to this, I'd probably just do a loop to step through it since pretty much all date mathematics are unavailable.
Something like this will probably do it:
$now = strtotime('now');
$later = strtotime('next week', $now);
$format = 'F j, Y';
$step = '+1 day';
while ($now < $later) // maybe <=?
printf("Date: %s<br />", date($format, $now));
$now = strtotime($step, $now);
Now, to do the same when DatePeriod is available, methinks its like so:
$now = new DateTime();
$later = new DateTime('next week');
$period = new DatePeriod($now, 'P1D', $later);
foreach ($period AS $day)
print $day->format('F j, Y');
DateTime would be useful, except that without any of the subtraction or difference methods it becomes a little on the useless side.