繁体   English   中英

PHP中的date_format()不返回格式化日期

[英]date_format() in PHP not returning the formatted date

date_format函数不返回变量的格式化值,而仅返回当前日期

我尝试了不同的日期变量和格式,但无济于事。 这是在WordPress环境中完成的。

....

 <span class="some-css-class">
  <?php
   $eventDate = new DateTime(the_field('date_event'));
   //'event_date is the name of the date time field created for the post
   echo date_format($eventDate,'M'); 
  ?>
  </span>

....

我尝试了不同的变量和数字格式。 搜索了特定于WordPress环境的解决方案,但没有一个有效。 结果显示格式为今天的日期,而不是给定的日期。

结果以非格式形式显示事件的日期,并以格式形式显示今天的日期。

结果显示格式为今天的日期,而不是给定的日期。

那是因为您在这里没有给DateTime构造函数指定任何日期-因此它默认为今天。

$eventDate = new DateTime(the_field('date_event'));

the_field直接输出值。 您要将值作为参数传递给DateTime构造函数,因此需要使用返回值而不是直接输出值的函数get_field

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM