0

Currently I have the following string.

$timeago = human_time_diff( get_the_time('U'), current_time('timestamp') );
print $timeago;

This returns results that looks like this.

1 min, 1 hour, 1 week, 1 month, and 1 year.

I am trying to figure out how I can make this work using an if statement to detect if the post has been posted within the past 5 hours, and if it has make it echo "NEW" but if not, don't echo anything.

EDIT: I tried the following with no success... I am getting confused on how to make it check for the hours portion as well as the number portion I guess.

$timeago = human_time_diff( get_the_time('U'), current_time('timestamp') );
print $timeago;
if( $timeago >= 0 && $timeago <= 5 )
{
    print 'NEW';
}

1 Answer 1

2

human_time_difference() will not work here as it returns a string in a human readable form. However, we can use some of the logic used there to construct a workable function

You can try the following: (Untested)

function get_custom__time_diff()
{
    global $post;

    // Get the current time
    $current_time = time();
    // Get the post date
    $post_date    = get_the_time( 'U', $post );
    // Get the amount of seconds in 5 hours
    $test_time    = 5*HOUR_IN_SECONDS;
    $diff         = $current_time - $post_date;

    if ( $diff < $test_time )
        return 'New';

    return '';
}

You can then call it as follow inside the loop

echo get_custom__time_diff();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.