if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' ) ) {
echo 'New!';
}我也试过:
if ( get_the_date( 'U' ) > strtotime( '-7 days' ) )
if ( get_the_date( 'U' ) < strtotime( '-7 days' ) )
if ( get_the_date( 'U' ) > strtotime( '7 days' ) )
if ( get_the_date( 'U' ) < strtotime( '7 days' ) )
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) > strtotime( '-7 days' ) )
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '-7 days' ) )
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) > strtotime( '7 days' ) )
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' ) )基本的想法是,如果一个博客还不到7天,它仍然被认为是新的。
我在循环中工作,但也许我的逻辑是错的?
发布于 2021-06-30 16:24:04
human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) )生成文章发布时到当前时间作为人类可读的字符串(如'6 days' )之间的时间。同时,strtotime( '7 days' )检索一个整数时间戳,表示这一时刻之后的7天,例如1625673951。
考虑到这一点,我们可以考虑一下你的比较表达式,
human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' )评估为类似于
'6 days' < 1625673951在此比较中,PHP尝试将字符串转换为整数,以便将其与时间戳进行比较,在本例中,'6 days'被转换为整数6。
比较两个日期最简单的方法之一就是比较它们的整数时间戳。在这里,我们可以比较一下邮局的时间戳是否大于这一时刻前7天的时间戳,这表明它是在上周内发布的:
if( get_the_time( 'U' ) > strtotime( '-7 days' ) )https://wordpress.stackexchange.com/questions/391182
复制相似问题