2014-06-18 49 views
3

您好,我有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。鏈接給出的月份是動態的,它可以是任何數字。

回答

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.''; 
+0

thxn轉換,這是IW的期待 – user3058067

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

快樂編碼!

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