2015-09-04 156 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')); 
相关问题