Support » Plugins and Hacks » Hacks » Comparing Post Date with Current Date and Displaying Upcoming Events

Viewing 9 replies - 1 through 9 (of 9 total)
  • esmi

    (@esmi)

    Forum Moderator

    Both $postDate and $todaysDate are just strings (as opposed to actual date values). You’ll need to convert them into real dates for comparison using something like mktime().

    Looking up mktime() led me to this in the Codex: http://codex.wordpress.org/Function_Reference/current_time

    If I change the variables to

    $postDate = get_the_date();
    $todaysDate = current_time('mysql');

    And echo these two variables, it outputs the same for each.

    I’m still not sure how to make this work with my code (working with dates confuses me!).

    Try this

    $postDate = strtotime( $post->post_date );
    $todaysDate = time();

    Also there is no need to set todays date inside loop. You can do it once before foreach.

    Just noticed:
    $postDate > $todaysDate | $postDate = $todaysDate

    I think it should be
    $postDate >= $todaysDate
    or
    $postDate > $todaysDate || $postDate == $todaysDate

    Hi Oleg,

    Thanks for helping me, as well. Sorry, but how should I modify the code? I understand to replace the variables with the ones I’ve suggested, but how should I modify the loop?

    Here’s what I’ve changed so far:

    <?php
    global $post;
    $args = array('category' => 9, 'numberposts' => 4,'orderby' => 'date','order' => 'ASC',);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <?php
    $postDate = strtotime( $post->post_date );
    $todaysDate = time();
    if ( $postDate > $todaysDate | $postDate = $todaysDate) : ?>
        <li><a href="<?php the_permalink(); ?>" style="font-size:11px;"><span style="font-weight:bold;color:#afaca5"><?php the_time('n/j'); ?>:</span> <?php the_title_attribute(); ?></a></li>
    <?php endif; ?>
    <?php endforeach; ?>
    </ul>
    <br />

    Should I remove this line?:

    if ( $postDate > $todaysDate | $postDate = $todaysDate) : ?>

    Right now, when I run the modified code with the new variables, nothing is showing up in the sidebar box – is there a syntax error someplace? Thanks for helping me – I’m not very clear about how to do this.

    Sorry, just saw your follow-up post and will try that now.

    Hi Oleg,

    The widget is displaying all my future events! Thanks so much. I would never have figured this out on my own.

    However, when I enter a post and date it today, the post doesn’t show up in the widget and the widget only shows 3 upcoming events (versus the 4 that is specified), but if I delete the post dated today, all 4 upcoming events show up.

    I tried:

    $postDate >= $todaysDate
    and
    $postDate > $todaysDate || $postDate == $todaysDate

    Any ideas?

    Thanks!

    Hi,
    Try next code:
    $todaysDate = time() - (time() % 86400);

    I see the source of problem.
    'numberposts' => 4
    you limit number of posts to 4.
    Let’s say you have 4 future posts and 1 today’s post. = 5 in total.
    get_posts will return only 4 posts : 3 future and 1 for today.
    *today* means time when post was created, which of’course will be less than current time.
    So we need to adjust todays time to be 1 second past yesterday’s midnight.

    It’s working perfectly now. Thank you so much!!

    And here’s the final code in case anyone else needs something like this:

    <?php
    global $post;
    $args = array('category' => 9, 'numberposts' => 4,'orderby' => 'date','order' => 'ASC',);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <?php
    $postDate = strtotime( $post->post_date );
    $todaysDate = time() - (time() % 86400);
    if ( $postDate >= $todaysDate) : ?>
        <li><a href="<?php the_permalink(); ?>" style="font-size:11px;"><span style="font-weight:bold;color:#afaca5"><?php the_time('n/j'); ?>:</span> <?php the_title_attribute(); ?></a></li>
    <?php endif; ?>
    <?php endforeach; ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Comparing Post Date with Current Date and Displaying Upcoming Events’ is closed to new replies.