如果开始日期和结束日期包括两个月,则将日期格式更改为 "d M - d M Y"
Change date format to "d M - d M Y" if start date and end date include two months
我正在使用 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
”。
我怎样才能做到这一点?
您可以借助 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>
我正在使用 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
”。
我怎样才能做到这一点?
您可以借助 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>