首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果自从“邮报”出版以来,X时间已经过去了,那就做点什么吧

如果自从“邮报”出版以来,X时间已经过去了,那就做点什么吧
EN

WordPress Development用户
提问于 2021-06-29 18:40:05
回答 1查看 92关注 0票数 0
代码语言:javascript
复制
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' ) ) {
    echo 'New!';
}

我也试过:

代码语言:javascript
复制
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天,它仍然被认为是新的。

我在循环中工作,但也许我的逻辑是错的?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2021-06-30 16:24:04

当前代码做什么

代码语言:javascript
复制
human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) )

生成文章发布时到当前时间作为人类可读的字符串(如'6 days' )之间的时间。同时,strtotime( '7 days' )检索一个整数时间戳,表示这一时刻之后的7天,例如1625673951

考虑到这一点,我们可以考虑一下你的比较表达式,

代码语言:javascript
复制
human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' )

评估为类似于

代码语言:javascript
复制
'6 days' < 1625673951

在此比较中,PHP尝试将字符串转换为整数,以便将其与时间戳进行比较,在本例中,'6 days'被转换为整数6

A解决方案

比较两个日期最简单的方法之一就是比较它们的整数时间戳。在这里,我们可以比较一下邮局的时间戳是否大于这一时刻前7天的时间戳,这表明它是在上周内发布的:

代码语言:javascript
复制
if( get_the_time( 'U' ) > strtotime( '-7 days' ) )
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/391182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档