In PHP given a month string such as “November” how can I return 11 without using a 12 part switch statement?

前端 未结 6 1046
攒了一身酷
攒了一身酷 2020-12-06 12:20

I.e.

Month        Returns
January      1
February     2
March        3
April        4
May          5
June         6
July         7
August       8
September           


        
6条回答
  •  萌比男神i
    2020-12-06 13:11

    Depending on your scenario probably the most efficient solution to this would be to create an associative array like:

    $months = array("january" => 1, "february" => 2, "march" => 3, "april" => 4, "may" => 5, "june" => 6, "july" => 7, "august" => 8, "september" => 9, "october" => 10, "november" => 11, "december" => 12);
    

    And then your lookups are super easy:

    $month = "Nobember";
    $monthNumber = $months[strtolower($month)];
    

提交回复
热议问题