PHP: Populating an array with the names of the next 12 months

后端 未结 8 2192
遇见更好的自我
遇见更好的自我 2020-12-03 18:45
for($x=0; $x<12; $x++)
{
    $month = mktime(0, 0, 0, date(\"m\")+$x, date(\"d\"),  date(\"Y\"));
    $key = date(\'m\', $month);
    $monthname = date(\'F\', $mo         


        
8条回答
  •  余生分开走
    2020-12-03 19:21

    "Result is that $months would result in an array where 07 = July 08 = August, 09 = September."

    for ($key = 1; $key <=12; $key++) {
        $months[str_pad($key, 2, '0', STR_PAD_LEFT)] = date('F', strtotime('2000-' . $key));
    }
    

    If you're okay with 7 = July 8 = August, 9 = September, then:

    for ($key = 1; $key <=12; $key++) {
        $months[$key] = date('F', strtotime('2000-' . $key));
    }
    

提交回复
热议问题