如何将月份本地名转换为相应月份号?PHP:将本地月份名转换为月份号
您可以手动进行翻译,但我很确定PHP有一些内置的函数为此?
$monthname = "februar"; // norwegian name for february
$monthnumber = insertCorrectFunctionHere($monthname);
的目标是$monthnumber
成为“2”在这里(或“02”),因为二月是第二个月份。
如何将月份本地名转换为相应月份号?PHP:将本地月份名转换为月份号
您可以手动进行翻译,但我很确定PHP有一些内置的函数为此?
$monthname = "februar"; // norwegian name for february
$monthnumber = insertCorrectFunctionHere($monthname);
的目标是$monthnumber
成为“2”在这里(或“02”),因为二月是第二个月份。
首先,你需要使用的strtotime功能
的strtotime函数的输出可以传给日期函数生成月份当月转换为时间格式的数值格式
您可以使用下面的代码
$date = 'january';
echo date('m', strtotime($date));
上面的代码的输出将是01参考
只需使用date()函数。使用下面的代码
<?php
$month_number = date('m', strtotime('1 February'));
echo $month_number; //Prints 02
另一种方式,你可以做一个数组。使用下面
<?php
$array = array("Januar"=>"01","Februar"=>"02","Mars"=>"03","April"=>"04","Mai"=>"05","Juni"=>"06","Juli"=>"07","August"=>"08","September"=>"09","Oktober"=>"10","November"=>"11","Desember");
$month_name = "February";
$month = ucwords(strtolower("Februar"));
$month_number = $array[$month];
echo $month_number; // Prints 02
希望这段代码可以帮助您
对于date()方法,如果当前日期在闰年大于28或29,则将返回3月。所以为了在2月1日将这个用作字符串。与没有31天的月份一样。 – 2015-02-09 13:20:53
@Matt Burrow感谢您告诉我更新了我的答案 – 2015-02-09 13:22:27
它不适用于挪威月份名称,即使将locale设置为nowegian也是如此。 – 2015-02-09 13:43:39
正确设置区域设置为挪威;使用[strptime](http://www.php.net/manual/en/function.strptime.php)从'1-Februar-2015'获取一个数组,然后读取该数组的tm_mon的值,加1到那个价值 – 2015-02-09 13:09:51
@Kristian Rafteseth看看我的回答,我认为它回答你的问题 – 2015-02-09 13:19:55