PHP 日期 - 以当地语言获取月份名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13845554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP date - get name of the months in local language
提问by Sasha
I have this part of the function, which gives me name of the months in English. How can I translate them to my local language (Serbian)?
我有这部分功能,它给了我英文月份的名称。我如何将它们翻译成我的当地语言(塞尔维亚语)?
$month_name = date('F', mktime(0, 0, 0, $i));
Where $iis the number of the month (values 1 - 12). See also PHP:mktime.
哪里$i是月份数(值 1 - 12)。另见PHP:mktime。
回答by MAXIM
You should use setlocale():
你应该使用setlocale():
setlocale(LC_TIME, 'fr_FR');
$month_name = date('F', mktime(0, 0, 0, $i));
In this case it would set it to French. For your case it should be one of the following:
在这种情况下,它会将其设置为法语。对于您的情况,它应该是以下之一:
sr_BA- Serbian (Montenegro)sr_CS- Serbian (Serbia)sr_ME- Serbian (Serbia and Montenegro)
sr_BA- 塞尔维亚语(黑山)sr_CS- 塞尔维亚语(塞尔维亚)sr_ME- 塞尔维亚语(塞尔维亚和黑山)
回答by Arnold Daniels
You should use setlocale()and strftime():
你应该使用setlocale()和strftime():
setlocale(LC_TIME, 'sr_CS');
$month_name = strftime('%B', mktime(0, 0, 0, $i));
回答by Arnaud
Here is an example with IntlDateFormatter
这是一个IntlDateFormatter的例子
$format = new IntlDateFormatter('sr_CS', IntlDateFormatter::NONE,
IntlDateFormatter::NONE, NULL, NULL, "MMM");
$monthName = datefmt_format($format, mktime(0, 0, 0, $i));
回答by Kai Noack
For all who struggle with German (and de_DE), make sure you are using the right language code. Login to your server and run locale -ato see a list of all available ones. For me it shows:
对于所有与德语(和de_DE)斗争的人,请确保您使用正确的语言代码。登录到您的服务器并运行locale -a以查看所有可用服务器的列表。对我来说,它显示:
C
C.UTF-8
de_AT.utf8
de_BE.utf8
de_CH.utf8
de_DE.utf8
de_LI.utf8
de_LU.utf8
...
C
C.UTF-8
de_AT.utf8
de_BE.utf8
de_CH.utf8
de_DE.utf8
de_LI.utf8
de_LU.utf8
...
You need to use one of those codes.
您需要使用这些代码之一。
Then you can use:
然后你可以使用:
date_default_timezone_set('Europe/Berlin');
setlocale(LC_ALL, 'de_DE.utf8');
$date_now = date('Y-m-d');
$month_available = strftime('%B %Y', strtotime($date_now));
$month_next = strftime('%B %Y', strtotime($date_now.' +1 month'));
and "M?rz 2020" etc. get displayed correctly.
和“M?rz 2020”等得到正确显示。
回答by Bud Damyanov
It is good idea to pass the encoding when setting the locale:
在设置语言环境时传递编码是个好主意:
<?php
date_default_timezone_set('Europe/Belgrade');
setlocale(LC_TIME, array('sr_CS.UTF-8', 'sr.UTF-8'));

