PHP:日期 "F",setlocale 无效
PHP: date "F" with setlocale not working
<ul>
<?php
$event_date = get_sub_field('event_date'); // 20150203
$event_date_format = DateTime::createFromFormat('Ymd', $event_date);
setlocale(LC_ALL, 'de_DE');
?>
<li>
<h6><?php echo $event_date_format->format('d');?>. <?php echo $event_date_format->format('F');?></h6>
<p><?php echo $event_date_format->format('H');?>:<?php echo $event_date_format->format('i');?></p>
</li>
</ul>
这个输出是
03. February
19:25
为什么setlocale对此没有任何影响。我希望我的月份 "F" 像 3. Feber 19:25
一样用德语
知道我做错了什么吗?
更新 1:
如果我尝试使用 strftime()
,我会突然得到一个不同的日期输出。它是德语,但错了?
<ul>
<?php
$event_date = get_sub_field('event_date');
$event_date_format = DateTime::createFromFormat('Ymd', $event_date);
setlocale(LC_ALL, 'de_DE');
?>
<li>
<h6><?php echo $event_date_format->format('d');?>. <?php echo strftime('%B', $event_date);?></h6>
<p><?php echo $event_date_format->format('H');?>:<?php echo $event_date_format->format('i');?></p>
</li>
</ul>
突然输出日期不是03. February
而是03. August
,即使日期应该是二月。
有什么想法吗?
更新 2:
这很奇怪。我刚刚在 unix 转换工具中在线检查了变量 $event_date
,我得到了这个……
$event_date: 20150203
Sat, 22 Aug 1970 05:16:43 GMT
该值是在带有日期选择器的 wordpress 后端中设置的,并且明确表示 03/02/2015
date()
和 DateTime 不遵守语言环境
使用strftime()
http://php.net/manual/en/function.strftime.php
http://php.net/manual/en/function.date.php
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date().
烦人的是,date
不支持语言环境。来自手册:
To format dates in other languages, you should use the setlocale() and
strftime() functions instead of date.
你可能想要:
setlocale(LC_TIME, 'de_DE');
echo strftime('%B');
或者,结帐 Carbon。它具有一致的 API,包括区域设置感知。例如:
setlocale(LC_TIME, 'German');
echo Carbon::now()->formatLocalized('%B'); // Sonntag
setlocale(LC_TIME,'de_DE.UTF8');
$eventDate = new DateTime(get_field('event_date'));
$eventDate_ts = $eventDate->getTimestamp();
echo gmstrftime("%B", $eventDate_ts);
字符编码设置为'de_DE.UTF8' for German, gmstrftime is used here, also。
March to März, Mar to Mrz.
DateTime() 的成员函数生成必要的时间戳。
<ul>
<?php
$event_date = get_sub_field('event_date'); // 20150203
$event_date_format = DateTime::createFromFormat('Ymd', $event_date);
setlocale(LC_ALL, 'de_DE');
?>
<li>
<h6><?php echo $event_date_format->format('d');?>. <?php echo $event_date_format->format('F');?></h6>
<p><?php echo $event_date_format->format('H');?>:<?php echo $event_date_format->format('i');?></p>
</li>
</ul>
这个输出是
03. February
19:25
为什么setlocale对此没有任何影响。我希望我的月份 "F" 像 3. Feber 19:25
知道我做错了什么吗?
更新 1:
如果我尝试使用 strftime()
,我会突然得到一个不同的日期输出。它是德语,但错了?
<ul>
<?php
$event_date = get_sub_field('event_date');
$event_date_format = DateTime::createFromFormat('Ymd', $event_date);
setlocale(LC_ALL, 'de_DE');
?>
<li>
<h6><?php echo $event_date_format->format('d');?>. <?php echo strftime('%B', $event_date);?></h6>
<p><?php echo $event_date_format->format('H');?>:<?php echo $event_date_format->format('i');?></p>
</li>
</ul>
突然输出日期不是03. February
而是03. August
,即使日期应该是二月。
有什么想法吗?
更新 2:
这很奇怪。我刚刚在 unix 转换工具中在线检查了变量 $event_date
,我得到了这个……
$event_date: 20150203
Sat, 22 Aug 1970 05:16:43 GMT
该值是在带有日期选择器的 wordpress 后端中设置的,并且明确表示 03/02/2015
date()
和 DateTime 不遵守语言环境
使用strftime()
http://php.net/manual/en/function.strftime.php
http://php.net/manual/en/function.date.php
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
烦人的是,date
不支持语言环境。来自手册:
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date.
你可能想要:
setlocale(LC_TIME, 'de_DE');
echo strftime('%B');
或者,结帐 Carbon。它具有一致的 API,包括区域设置感知。例如:
setlocale(LC_TIME, 'German');
echo Carbon::now()->formatLocalized('%B'); // Sonntag
setlocale(LC_TIME,'de_DE.UTF8');
$eventDate = new DateTime(get_field('event_date'));
$eventDate_ts = $eventDate->getTimestamp();
echo gmstrftime("%B", $eventDate_ts);
字符编码设置为'de_DE.UTF8' for German, gmstrftime is used here, also。
March to März, Mar to Mrz.
DateTime() 的成员函数生成必要的时间戳。