Support

Account

Home Forums Front-end Issues Hide Passed Dates

Solving

Hide Passed Dates

  • I have a loop now that shows a series of dates (and other fields) and want to hide dates that have passed.

    One of my date fields is called “date_one” and it uses the jquery date field type.

    I’ve found various PHP tutorials on comparing dates, but can’t quite put it together with my loop/ACF.

    Can anyone share how I would structure this? In my one loop I have 14 dates, and need to hide each of them if the dates are prior to today’s date.

    My loop looks like this (simplified)

    <?php query_posts('&orderby=rand&posts_per_page=-1&post_type=listing&cat=41'); ?>
    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php if( get_field('date_one') ) {?>
    <?php the_field('date_one'); ?>
    <?php } else { ?>
    <?php } ?>
    
    <?php if( get_field('date_two') ) {?>
    <?php the_field('date_two'); ?>
    <?php } else { ?>
    <?php } ?>
    
    <!-- This continues for all 14 dates -->
    <?php endwhile; ?>
  • Hi.

    You will need to check the values of each date and compare it with the current date.

    First, declare a variable with the current unix time before you run through all the date checks.

    $now = time();

    This will save the unix time stamp which is a long number that makes no sense until converted into a human readable format. Basically, its the number of seconds from the UNIX epoch, or January 1st, 1970.

    This value will automatically update everytime the function is called of course.

    Now, between each check if a value exists for a key, don’t immediately print the field value with the_field(‘key’) function. Instead, compare it now to the $now variable you just declared. Compare it like this:

    
    $date_one_timestamp = strtotime(get_field('date_one));
    if ($now < $date_one_timestamp ) {
    // Since the the date is ahead of the absolute second of now
    // we will echo the timestamp using the date function, and format it like
    // "January 20, 2014"
    
    echo date("F j, Y", $date_one_timestamp);
    
    } else {
    // do nothing or do something else
    // because the unix time stamp of the date is greater then this second in time
    }
    

    Now, this assumes that for the date field in ACF, you saved it using a format that can be read by the php strtotime function. More on that can be read here:
    http://us3.php.net/manual/en/function.strtotime.php

    But as you will read, the best way to format it is YYYY-MM-DD
    Since that will keep from ambiguity when its trying to figure out what each numbers mean.

    Hope that helps.

    Last bit, for more efficient code and presentation for yourself/clients when delcaring these dates, consider purchasing the Repeater Field addon. Then again, @elliot is about to turn all addons into a complete ACF Pro plugin, so maybe hold off too. Repeater addon is great, as is the rest of the addons.

    Btw, I don’t work for Elliot, just helping out. This is seriously the best plugin available for WP and powers 2 of my projects that I have highly customized.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Hide Passed Dates’ is closed to new replies.