• Hey everyone,

    I’m trying to add a “NEW” text next to the title of each post if the published date is within the last 7 days. Here is what I currently have:

    <?php if (post_date > strtotime('-7 days')): ?> NEW <?php endif; ?>

    What am I missing? Not sure if I’m referencing things properly.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • strtotime results in a timestamp (number of seconds since January 1, 1970, while post_date isn’t. You’re comparing apples to oranges.

    You should convert post_date also with strtotime.

    Peter

    Thread Starter surfacinglove

    (@surfacinglove)

    I’ve tried this, and the functionality still isn’t working properly. Looking at the following example:

    Return posts for various date ranges:

    <?php
    //based on Austin Matzko's code from wp-hackers email list
      function filter_where($where = '') {
        //posts in the last 30 days
        //$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        //posts  30 to 60 days old
        //$where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'";
        //posts for March 1 to March 15, 2009
        $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    How can I take what I have above and use the same functionality from this example in an if statement to add something extra if the date range is true? I still can’t get it quite right…

    Thread Starter surfacinglove

    (@surfacinglove)

    Found the format I needed:

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘If statement – Comparing post date to today’s date’ is closed to new replies.