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

守給你的承諾、 提交于 2019-11-26 21:37:26

问题


I.e.

Month        Returns
January      1
February     2
March        3
April        4
May          5
June         6
July         7
August       8
September    9
October      10
November     11
December     12

I've seen examples using mktime when given the number of the month and returning the month string, but not the reverse.


回答1:


Try

echo date('n', strtotime('November')); // returns 11

If you have to do this often, you might consider using an array that has these values hardcoded:

$months = array( 1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
                 5 => 'May',     6 => 'June',     7 => 'July',  8 => 'August',
                 9 => 'September', 10 => 'October', 11 => 'November',
                 12 => 'December');

Can also do it the other way round though, using the names for the keys and numbers for values.

With the names for values you do

echo array_search('November', $months); // returns 11

and with names for keys you do

echo $months['November']; // returns 11

I find using the numbers for the keys somewhat better in general, though for your UseCase the names for keys approach is likely more comfortable. With just 12 values in the array, there shouldn't be much of a difference between the array approches.

A quick benchmark noted a difference of 0.000003s vs 0.000002s, whereas the time conversion takes 0.000060s on my computer (read: might differ on other computer).




回答2:


What about using strtotime() to convert November to a timestamp, and, then, the date() function with the n format to get the corresponding number :

$ts = strtotime('november');
echo date('n', $ts);

Gives the following output :

11


And, just for fun, a portion of code such as the following one :

$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', );
foreach ($months as $m) {
    $ts = strtotime($m);
    echo $m . ' : ' . date('n', $ts) . '<br />';
}

will give you the list of all month with their corresponding numbers -- showing that this idea will work for all 12 months, and not only November ;-)




回答3:


I can't imagine this is more efficient, but it works

$t = strtotime("March");
echo date('n',$t);



回答4:


The only thing I can think of off the top of my head is to set each string as a variable and make it = the respective number. But then again I'm pretty new, so I'm eager to see as well if there are any easier ways (=




回答5:


$months = array('January', 'February', 'March', 'April', 'May', 'June', 
                'July', 'August', 'September', 'October', 'November', 'December', );
foreach ($months as $m) {
    echo $m."=".date('n', strtotime($m))."<br>";
};



回答6:


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)];


来源:https://stackoverflow.com/questions/2701695/in-php-given-a-month-string-such-as-november-how-can-i-return-11-without-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!