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
Why does setlocale not have any impact on this. I want my Month "F" to be in german like 3. Feber 19:25
你知道我在做什么错吗?
Any idea what I'm doing wrong here?
更新1:
如果我尝试使用strftime()
,我会突然得到不同的日期输出.这是德语,但是错了吗?
If I try using strftime()
i suddenly get a different date output. it is in german but wrong?
<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月.
Suddenly the output date is not 03. February
but 03. August
, even though the date should be February.
有什么想法吗?
更新2:
这很奇怪.我刚刚在Unix转换工具中在线检查了变量$event_date
,我得到了这个...
This is pretty weird. I just checked the variable $event_date
online in a unix conversion tool and the I get this …
$ event_date:20150203
$event_date: 20150203
Sat, 22 Aug 1970 05:16:43 GMT
该值设置在带日期选择器的wordpress后端内部,并清楚显示2015年3月2日
The value is set inside a wordpress backend with a datepicker and clearly says 03/02/2015
推荐答案
date()
和DateTime不遵守语言环境
date()
and DateTime do not respect locale
使用strftime()
http://php.net/manual/en/function.strftime.php
http://php.net/manual/en/function.date.php
要使用其他语言设置日期格式,应使用setlocale()和 strftime()函数代替date()函数.
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
这篇关于PHP:日期"F"与setlocale不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!