WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Comparing Post Date with Current Date and Displaying Upcoming Events (10 posts)

  1. web-girl
    Member
    Posted 2 years ago #

    I have a sidebar widget that will display upcoming events (Posts categorized as events). I only want to show events that are occurring today or in the future and have them order by soonest to latest.

    However, I can't seem to figure out how to have these events display properly. I tried a bunch of solutions I found through Google, but it is still not working. Can someone help?

    Here's the code:

    <?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 = get_the_date();
    $todaysDate = date('j F Y');
    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 />
  2. esmi
    Forum Moderator
    Posted 2 years ago #

    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().

  3. web-girl
    Member
    Posted 2 years ago #

    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!).

  4. Oleg Dudkin
    Member
    Posted 2 years ago #

    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.

  5. Oleg Dudkin
    Member
    Posted 2 years ago #

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

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

  6. web-girl
    Member
    Posted 2 years ago #

    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.

  7. web-girl
    Member
    Posted 2 years ago #

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

  8. web-girl
    Member
    Posted 2 years ago #

    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!

  9. Oleg Dudkin
    Member
    Posted 2 years ago #

    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.

  10. web-girl
    Member
    Posted 2 years ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic