2015-09-04 149 views
2

在我的項目中,我將英文日期保存在數據庫中作爲'Y-m-d'。現在,我想以2015年5月28日的格式顯示西班牙語日期。我該怎麼做呢?我嘗試了以下,但無濟於事。如何在PHP中將英文日期更改爲西班牙文?

setlocale(LC_TIME, 'spanish'); 
echo utf8_encode(strftime("%d %B, %Y",strtotime($date))); 

當我打印setlocale時,它返回bool(false)。是否有任何其他方式來做到這一點?

+0

我想簡單的日期和的strtotime將解決這一 –

+1

'的setlocale(LC_TIME,「es_ES」);' – Zl3n

回答

-1

這應該工作:

echo date("d M, Y", strtotime($date)); 
+0

了不支持的區域設置在OP的問題,這就造成了'FALSE'返回值中所述使用['setlocale()']手冊(http://php.net/manual/en/function.setlocale.php#refsect1-function.setlocale-returnvalues) – fyrye

0

你應該使用這樣的:

setlocale(LC_TIME, 'es_ES'); 

// or (to avoid utf8_encode) : setlocale(LC_TIME, 'es_ES.UTF-8'); 
0

我建議利用intl庫函數來代替,如IntlDateFormatter。這將允許您輸出本地化數據,而無需使用setlocale()更改全局區域設置。

intl庫還可以讓你查看支持的語言環境的列表,你可以使用var_dump(ResourceBundle::getLocales(''));

例:https://3v4l.org/BKCRo(注意如何setlocale(LC_ALL, 'es_ES')對輸出沒有影響

$esDate = datefmt_create('es_ES', //output locale 
    \IntlDateFormatter::FULL, //date type 
    \IntlDateFormatter::NONE, //time type 
    'America/Los_Angeles', //time zone 
    IntlDateFormatter::GREGORIAN, //calendar type 
    'dd LLLL, YYYY'); //output format 
echo $esDate->format(new \DateTime); 

結果:

18 diciembre, 2017 

有關支持的日期格式patt ERNS看到:http://userguide.icu-project.org/formatparse/datetime


至於記下setlocale(),每個系統都是不同的,並不是所有的語言環境可以通過你的PHP的分佈和服務器操作系統的支持。

使用Linux時,可以使用控制檯終端上的locale -a或PHP中的system('locale -a', $locales); var_dump($locales);來確定支持的系統區域設置。

使用Windows時,您可以導航至Control Panel->LanguageControl Panel->International Settings來查看系統支持的語言環境。 請參閱https://msdn.microsoft.com/en-us/library/cc233982.aspx瞭解各種Windows版本支持的區域設置的更多詳細信息。

如果使用setlocale(),確保按照從左到右的優先順序爲所需區域設置提供所有可能的變體,以減少返回false的可能性。

例如

setlocale(LC_TIME, array('es_ES.UTF-8', 'es_ES', 'es-ES', 'es', 'spanish', 'Spanish')); 
相關問題