How to Display Today and Tomorrow Instead of Post Dates (4 posts)

  1. web-girl
    Posted 4 years ago #

    I seem to always get stuck working with dates and I can't get this query to work properly. I'm still learning how to write these queries, so thanks for your patience.

    Here's what I'd like (this is for displaying Posts tagged as events)
    - If a custom field is filled in, I'd like it to display the contents of the custom field.
    However, if the custom field is blank, I'd like it to do the following, but only if the parent category is events:
    - If the post's date is today's date, then I would like it to say Today
    - If the post's date is tomorrow's date, then I would like it to say Tomorrow
    - If the post's date is neither, I'd like it to just display the date

    Thanks for your help!

    $postDate = strtotime( $post->post_date );
    $todaysDate = time() - (time() % 86400);
    $tomorrowsDate = time() - (time() % 172800);
    $eventtext = get_post_meta($post->ID, '_ct_text_4e3318cbab83d', true);
    if($eventtext) :
    echo get_post_meta( $post->ID, '_ct_text_4e3318cbab83d', true );
    elseif( $eventtext == '' || (is_category_or_sub(events)) || $postDate == $todaysDate ) :
    echo "Today";
    elseif( $eventtext == '' || (is_category_or_sub(events)) || $postDate == $tomorrowsDate ) :
    echo "Tomorrow";
    else( $eventtext == '' || (is_category_or_sub(events)) ) :
    echo the_date('M j', '', '');
    endif; ?>
  2. Rebecca O'Brien
    Posted 4 years ago #

    You didn't specify how yours isn't working. One problem I can see is you're telling it to display 'Today' or 'Tomorrow' based on || (or). So, if the eventtext is empty OR it's in the category events OR the date's match, echo 'Today'. You want && (and) based on your criteria.

    Based on what you said, this is how I (think) I would do it.

    Basically, retrieve the desired meta key. If it returns, echo that. Otherwise, retrieve a list of the categories for the post and check to see if your category is in it. (get_the_category_list returns a string)

    If it's in there, convert the post date to some standard format (see php.net/date). Do the same for today (just use date) and tomorrow (a combination of date and strtotime). If there's a match in the strings, win! Otherwise, show the date.

    $custom = get_post_meta($post->ID, 'myKey', 'true');
    if($custom) {
    } elseif(stristr(get_the_category_list(', '), 'events')) {
       $eventDate = get_the_date('Y-m-d');
       $today     = date('Y-m-d');
       $tomorrow  = date('Y-m-d', strtotime('+24 hours'));
       if($eventDate == $today) {
          $date = 'Today';
       } elseif($eventDate == $tomorrow) {
          $date = 'Tomorrow';
       } else {
          $date = $eventDate;
  3. web-girl
    Posted 4 years ago #

    Thanks for your help! I am going to try using your set-up and will keep working on it.

  4. web-girl
    Posted 4 years ago #

    Thanks for your help! I just tried the code and it seems to be working perfectly!

Topic Closed

This topic has been closed to new replies.

About this Topic