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

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

    Less complicated, no loops, generic array keys:

    function stackoverflow_get_monthname($x){ 
        return date("F",mktime(NULL, NULL, NULL, (int)date("n") + ($x+1), NULL, NULL)); 
    }
    $months = array_map("stackoverflow_get_monthname", range(1,12) );
    var_dump($months);
    

提交回复
热议问题