WordPress.org

Forums

How do I check if a post was published later than a certain date? (6 posts)

  1. slobizman
    Member
    Posted 4 years ago #

    I know how to check the age of a post, relative to today's date, but now I would like to check if a post was published after a particular date.

    For example, say the cutoff date is 08/05/11, I need to be able to check if the post that is displaying on the page was posted later than 08/05/11.

    Anyone know how to do this? I know the template tag is get_the_date(), but I just don't know how to do the date manipulation.

    Thanks.

  2. Adam Losier
    Member
    Posted 4 years ago #

    <?php if (strtotime($post->post_date) > strtotime('-4 days')): ?> <b>NEW</b> <?php endif; ?>

    Taken from http://wordpress.org/support/topic/if-statement-comparing-post-date-to-todays-date?replies=4

    PHPs strtotime function parses just about any English textual datetime description into a Unix time-stamp that can then be compared, so today's date would be something like 1234 and yesterdays date is 1233 so you can use the code above to do your comparisons.

    More Info: http://php.net/manual/en/function.strtotime.php

  3. slobizman
    Member
    Posted 4 years ago #

    That's great. Thanks!!!

  4. slobizman
    Member
    Posted 4 years ago #

    I'm having a problem get the post date to work with the into strtotime() function.

    global $post;
         $post_secs = strtotime($post->post_date);

    That does not put any value into $post_secs. I first had it without the global $post and then tried adding it.

    I'm using this in a custom_functions function in Thesis.

  5. slobizman
    Member
    Posted 4 years ago #

    I also just tried use the_date() and get_the_date() with no luck.

  6. slobizman
    Member
    Posted 4 years ago #

    Okay, this interesting. I use EXEC PHP plugin so I can run PHP in a WP page. So I put the following into a page, and viola!, it grabbed the post date with get_the_date().

    <?php
    $this_post_date = get_the_date() ;
    $post_secs = strtotime($this_post_date);
    $cutoff_secs = strtotime('March 01 2009');
    echo $cutoff_secs;
    echo $post_secs;
    ?>

    However, when I place this code in the Thesis custom_functions file, the $post_secs is blank. I tried adding a global $post at the beginning, but the same thing happens.

    I'm so close, can someone tell me how to make it work in custom_functions? This doesn't make any sense to me.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags