将时间戳转换为Wordpress默认语言

时间:2016-01-26 09:05:51

标签: php wordpress

当我试图推出元数据时,我遇到了语言障碍。

$string_date = implode (';', get_post_meta( get_the_ID(), 'dbt_opleverdatum' ) );
$format =  get_option( 'date_format' );
$string_date = date($format, $string_date)

由于我将元数据保存为php时间戳,因此将其作为1451865600放置(例如2016年1月4日)。我拿起它,然后通过一个日期戳把它拿出来,我把它作为4 January 2016

但是,整个网站都是用荷兰语运行的,当我查看代码时,我会注意到检索发布日期的函数get_the_date()。但是这将输出4 januari 2016,因为它通过Wordpress中的设置自行输出。它注意到wordpress是荷兰语并考虑到了这一点。

是否有一个功能我可以将我的PHP日期通过,所以我的日期将用荷兰语而不是英语显示?

1 个答案:

答案 0 :(得分:1)

来自:http://php.net/manual/en/function.date.php

  

要使用其他语言格式化日期,您应该使用setlocale()和   strftime()函数而不是date()。

$ts = 1451865600;
$lang = get_option('WPLANG');
setlocale(LC_TIME, $lang);
$format2 = '%e %B %G';
$string_date = strftime($format2, $ts);

不幸的是,我不知道如何将选项中的date_format转换为strftime的格式字符串,但如果它是您的插件,那么您可以将其更改为strftime的格式,或者如果你也可以在其他地方使用它,然后为此添加另一个选项。

注意:使用setlocale会全局更改区域设置,因此在调用strftime之后可能需要恢复chage:

$current_locale = setlocale(LC_TIME, "0");
setlocale(LC_TIME, $lang);
...
setlocale(LC_TIME, $current_locale);
相关问题