admin管理员组文章数量:1289836
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' ) ) {
echo 'New!';
}
I've also tried:
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' ) )
The basic idea is if a blog post is still less than 7 days old, it's still considered new.
I'm working within the loop, but perhaps my logic is wrong?
if ( human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' ) ) {
echo 'New!';
}
I've also tried:
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' ) )
The basic idea is if a blog post is still less than 7 days old, it's still considered new.
I'm working within the loop, but perhaps my logic is wrong?
Share Improve this question edited Jun 30, 2021 at 15:29 asked Jun 29, 2021 at 18:40 user208381user208381 5 |1 Answer
Reset to default 0What the current code does
human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) )
produces the time between when the post is published and the current time as a human readable string such as '6 days'
. Meanwhile, strtotime( '7 days' )
retrieves a integer timestamp representing 7 days after this moment, e.g. 1625673951
.
With that in mind, we can consider your comparison expression,
human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) < strtotime( '7 days' )
to evaluate as something similar to
'6 days' < 1625673951
In this comparison, PHP tries it's best to convert the string into an integer so it can compare it to the timestamp, and in this case '6 days'
is cast as the integer 6
.
A solution
One of the easiest ways to compare two dates is to just compare their integer timestamps. Here, we can compare if the post's timestamp is greater than the timestamp of the time 7 days before this moment, indicating that it was published within the last week:
if( get_the_time( 'U' ) > strtotime( '-7 days' ) )
本文标签: dateIf X Amount of Time Has Passed Since Post Was PublishedDo Something
版权声明:本文标题:date - If X Amount of Time Has Passed Since Post Was Published, Do Something 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741473240a2380732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"6 days"
against the Unix timestamp for seven days after today. – bosco Commented Jun 29, 2021 at 18:48if( get_the_time( 'U' ) > strtotime( '-7 days' ) )
– bosco Commented Jun 29, 2021 at 19:27( get_the_date( 'U' ) > strtotime( '-7 days' ) )
to work. With thehuman_time_diff
versions you are comparing a "human" date to a non-human timestamp. – vancoder Commented Jun 29, 2021 at 21:57get_the_date( 'U' ) > strtotime( '-7 days' )
should indicate "new" - that comparison evaluates totrue
if the post's date is more recent (i.e. the post's timestamp is larger) then seven days ago. Reversing the comparison operator to invert the condition is fine - though usually you'll flip the "equal to" part of operation as well, e.g. the opposite of>
is<=
. In this case inverting the condition should not be necessary. – bosco Commented Jun 30, 2021 at 15:18