如何以我的语言显示月份名称

时间:2014-06-18 11:42:02

标签: php mysql

你好,我有2个月和年的变量。我想用我的语言显示月份名称,但我没有设法让它工作。这是我的代码

setlocale(LC_TIME, array('ro.utf-8', 'ro_RO.UTF-8', 'ro_RO.utf-8', 'ro', 'ro_RO', 'ro_RO.ISO8859-2')); 

$luna=$_GET['month'];
$an=$_GET['year'];

$dateObj   = DateTime::createFromFormat('!m', $luna);
$monthName = $dateObj->format('F');
$data='Arhiva '.$monthName.' - '.$an.'';

它显示2014年6月,我希望Iunie 2014年6月= Iunie在我的国家。链接给出的月份是dinamic,它可以是任何数字。

3 个答案:

答案 0 :(得分:3)

您可以使用strftimehttp://php.net/manual/en/function.strftime.php):

setlocale(LC_TIME, array('ro.utf-8', 'ro_RO.UTF-8', 'ro_RO.utf-8', 'ro', 'ro_RO', 'ro_RO.ISO8859-2')); 

$luna=$_GET['month'];
$an=$_GET['year'];

$dateObj   = DateTime::createFromFormat('!m', $luna);
$monthName = strftime('%B', $dateObj->getTimestamp());
$data='Arhiva '.$monthName.' - '.$an.'';

答案 1 :(得分:0)

试一下

setlocale(LC_TIME, 'fr_FR'); 
$monthName  = strftime("%B",mktime(0, 0, 0, $_GET['month']));
echo  $monthName ; //It outputs month name in your local language that you have set in 'setlocale'  

在这种情况下,它设置为法语。同样,您可以通过以下语言代码将其用于您的语言环境:

sr_BA - Serbian (Montenegro);
sr_CS - Serbian (Serbia);
sr_ME - Serbian (Serbia and Montenegro);

快乐的编码!!

答案 2 :(得分:0)

您可以使用IntlDateFormatter功能转换

$dateObj   = DateTime::createFromFormat('!m', $luna);

$formatter = new IntlDateFormatter("ro_RO",
                                    IntlDateFormatter::FULL, 
                                    IntlDateFormatter::FULL, 
                                    'Europe/Bucharest', 
                                    IntlDateFormatter::GREGORIAN,
                                    'MMMM');

echo $formatter->format($dateObj).' - '.$an; // Iunie - 2014