我肯定还有很多类似的问题,但我已经试过了很多,似乎不可能得到我想要的结果。 所以我想要的是转换时间戳在我的WordPress帖子显示“1分钟前,1天前,1周前等”,但只有当日期等于或少于1个月,否则显示正常的日期。
编辑:到目前为止,它只显示如下
这就是我想要的:
$posted = get_the_time('U');
if( (int)get_the_time( 'm' ) <= 1 ) {
echo human_time_diff($posted, current_time( 'U' )). " ago";
} else {
the_time('j F Y');
}
我相信,如果您将$posted
日期和字符串-1 month
转换为时间戳,那么您就可以检查$posted
的时间戳是否比一个月前的时间戳更大,即更近。
<?php
$posted = get_the_date();
$date = strtotime($posted) >= strtotime('-1 month') ? human_time_diff(strtotime($posted)) . ' ago' : $posted;
?>
<?php echo $date; ?>
对于小于1个月的日期,返回“时间之前”的预期日期格式,对于大于1个月的日期,返回“完整日期”格式。。。