What s the cleanest way to use a loop in PHP to list dates in the following way?
2011_10
2011_09
2011_08
2011_07
2011_06
...
2010_03
2009_02
2009_01
2009_12
2009_11
The key elements here:
- Should be as simple as possible - I would prefer one for loop instead of two.
- Should list this month s date as the first date, and should stop at a fixed point (2009-11)
- Should not break in the future (eg: subtracting 30 days worth of seconds will probably work but will eventually break as there are not an exact amount of seconds on each month)
Had to make a few tweaks to the solution:
// Set timezone
date_default_timezone_set( UTC );
// Start date
$date = date( Y ). - .date( m ). -01 ;
// End date
$end_date = 2009-1-1 ;
while (strtotime($date) >= strtotime($end_date))
{
$date = date ("Y-m-d", strtotime("-1 month", strtotime($date)));
echo substr($date,0,7);
echo "
";
}