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

后端 未结 8 2173
遇见更好的自我
遇见更好的自我 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:39

    An alternative would be to use strtotime:

    for ($x=0; $x < 12; $x++) {
    
        $time = strtotime('+' . $x . ' months', strtotime(date('Y-M' . '-01')));
        $key = date('m', $time);
        $name = date('F', $time);
        $months[$key] = $name;
    }
    

    In my opinion this code is easier to read.

提交回复
热议问题