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 You are comparing a string like "6 days" against the Unix timestamp for seven days after today. – bosco Commented Jun 29, 2021 at 18:48
  • That would give you the timestamp of seven days prior to today, sure :) . The other half is comparing that to the timestamp of the post, instead of a human readable string. E.g. if( get_the_time( 'U' ) > strtotime( '-7 days' ) ) – bosco Commented Jun 29, 2021 at 19:27
  • I'd expect ( get_the_date( 'U' ) > strtotime( '-7 days' ) ) to work. With the human_time_diff versions you are comparing a "human" date to a non-human timestamp. – vancoder Commented Jun 29, 2021 at 21:57
  • get_the_date( 'U' ) > strtotime( '-7 days' ) should indicate "new" - that comparison evaluates to true 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
  • We're all volunteers, it's cool, lol. It does seem like the problem might lay elsewhere. Perhaps edit in the rest of your loop? – bosco Commented Jun 30, 2021 at 15:38
Add a comment  | 

1 Answer 1

Reset to default 0

What 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