I want to list 12 months from today. Ther's missing a few months ..
$i = 12;
while ($i > 0) {
$ym = date('Y-m', strtotime("-$i month"));
$yms [$ym] = $ym;
$i--;
}
print_r($yms);
online example: http://codepad.org/XDv4iR3u
I want to list 12 months from today. Ther's missing a few months ..
$i = 12;
while ($i > 0) {
$ym = date('Y-m', strtotime("-$i month"));
$yms [$ym] = $ym;
$i--;
}
print_r($yms);
online example: http://codepad.org/XDv4iR3u
You forgot to provide the current ym
inside strtotime
. Consider this example:
$yms = array();
$now = date('Y-m');
for($x = 12; $x >= 1; $x--) {
$ym = date('Y-m', strtotime($now . " -$x month"));
$yms[$ym] = $ym;
}
echo "<pre>";
print_r($yms);
echo "</pre>";
Sample Output:
Array
(
[2013-05] => 2013-05
[2013-06] => 2013-06
[2013-07] => 2013-07
[2013-08] => 2013-08
[2013-09] => 2013-09
[2013-10] => 2013-10
[2013-11] => 2013-11
[2013-12] => 2013-12
[2014-01] => 2014-01
[2014-02] => 2014-02
[2014-03] => 2014-03
[2014-04] => 2014-04
)