【问题标题】:Change date format to "d M - d M Y" if start date and end date include two months如果开始日期和结束日期包括两个月,则将日期格式更改为“d M - d M Y”
【发布时间】:2017-08-12 19:01:42
【问题描述】:

我正在使用 ACF 转发器字段在我的 WordPress 网站中输出日期。

<h6 class="pegesus"><?php echo date("d", strtotime(get_sub_field('start_date'))).' - '.date("d M Y", strtotime(get_sub_field('end_date'))); ?></h6>

我得到的格式是“d - d M Y”,这是我需要的。

但如果开始日期和结束日期在两个月内,例如 3 月 29 日至 4 月 5 日,我想将格式更改为“d M - d M Y”。

我怎样才能做到这一点?

【问题讨论】:

  • 检查月份并使用其他格式字符串
  • 你能举个例子吗?我试着去做,但做不到

标签: php html wordpress date advanced-custom-fields


【解决方案1】:

您可以借助IF 和另一种日期格式来完成此操作。请遵循以下代码::

<h6 class="pegesus">
<?php
if(date("m", strtotime(get_sub_field('start_date'))) != date("m", strtotime(get_sub_field('end_date'))))
{
    echo date("d M", strtotime(get_sub_field('start_date'))).' - '.date("d M Y", strtotime(get_sub_field('end_date')));
}
else
{
    echo date("d", strtotime(get_sub_field('start_date'))).' - '.date("d M Y", strtotime(get_sub_field('end_date')));
}
?>
</h6>

【讨论】:

  • 感谢您的解决方案...您是救生员。
  • @Damon 如果它对您和其他人有帮助,也请为答案投票。
猜你喜欢
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2017-11-02
  • 2015-10-26
  • 2023-03-08
  • 2018-07-03
  • 1970-01-01
  • 2018-05-25
相关资源
最近更新 更多