2015-05-17 32 views
2
<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> 

的这个输出是PHP:日期 “F” 在与setlocale不工作

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. February03. August,即使日期应该是二月。

任何想法?


更新2:

这是很奇怪的。我只是检查变量$event_date在线在UNIX转换工具,我得到这个...

$ EVENT_DATE:20150203

Sat, 22 Aug 1970 05:16:43 GMT

的值设置一个WordPress后台里面有一个datepicker并明确表示说03/02/2015

enter image description here

+0

[日期时间::格式和strftime]的可能重复(http://stackoverflow.com/questions/16906937/datetimeformat-and-strftime) – bishop

+0

https://stackoverflow.com/questions/17567450/php-setlocale-doesnt-work/45848706#45848706 –

回答

2

date()和DateTime不尊重的语言环境

使用strftime()

http://php.net/manual/en/function.strftime.php

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

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

+0

谢谢,我更新了我的问题,并更改了<?php echo strftime('%B',$ event_date);?>'。但是我现在有这个问题,我的月份由于某种原因而不同?它应该是“Feber”(二月),而不是“八月”,因为它现在是输出。 – matt

+1

您开始使用$ event_date_format,但对于此调用,您仍然使用$ event_date,即字符串“20150302”。 strftime将它视为一个整数,在时代(又名Unix时间戳,自1970年1月1日以来的秒数)。你应该使用''strftime('%B',$ event_date_format-> getTimestamp())'' – fbas

0

令人烦恼的是,date不是区域意识。从手册:

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

你可能想:

setlocale(LC_TIME, 'de_DE'); 
echo strftime('%B'); 

另外,结账Carbon。它有一个包含语言环境意识的一致API。作为一个例子:

setlocale(LC_TIME, 'German'); 
echo Carbon::now()->formatLocalized('%B'); // Sonntag