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

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

    Sometime you need to be careful on your locale, so this is my solution (in a function):

    $months = [];
    
    for ($x=1; $x < 13; $x++) {
    
        $time = mktime(0, 0, 0, $x, 1);
        $key = date('m', $time);
        $name =  ucfirst(strftime('%B', $time));
        $months[(int)$key] = $name;
    }
    
    return $months;
    
    0 讨论(0)
  • 2020-12-03 19:45

    You might be getting the last day of the month (the 31st) bug - which led to two months with the same link - that Eddy very nicely figured out for me with this answer:

     $current_month = date('n');
    $MONTHS = array();
    for ($m=0; $m<12; $m++) {
      $display_month = $m + $current_month;
      $MONTHS[] = date('F',mktime(1,1,1,$display_month,1,date("Y")));
    
    0 讨论(0)
提交回复
热议问题