Support

Account

Home Forums Add-ons Repeater Field Compare dates in rows against today

Solved

Compare dates in rows against today

  • I have a repeater with rows containing dates.

    The dates are the start of each week.

    Is there a way to compare $today against the dates stored in the rows like below?
    <?php if( ($today >= $last_week) && ($today <= $next_week) ){ echo "id='active_week'"; }?>

  • Hi @phatoz

    I’m not sure I understand your query, so please forgive me if I’m wrong. Do you mean you want to check if the date you entered in the date field is within the range of 1 week (the past and future) from the current day? If that’s what you want, could you please try the following code?

    $today = date('Ymd');
    $last_week = $today-7;
    $next_week = $today+7;
    $date_field_value = get_field('date_picker', 85);
    
    if( ($date_field_value >= $last_week) && ($date_field_value <= $next_week) ){ 
        echo "id='active_week'";
    }

    Please keep in mind that you need to set the date field Return Format to “Ymd” on the field editor page.

    If that’s not what you want, could you please explain it and share some examples of the issue?

    Thanks 🙂

  • I’m very sorry for the time it has taken to respond to your reply.

    I’ve used your example with a few changes:

    // date starting the week of loaded treasure bag
    $treasure_bag_date = get_sub_field('treasure_bag_date', 85);
    $last_week = $treasure_bag_date-6;
    $next_week = $treasure_bag_date+6;
    
    <tr <?php if( ($treasure_bag_date >= $last_week) && ($treasure_bag_date <= $next_week) ){ echo "id='active_week'"; }?>>

    The code is echoing all the weeks.

    Here is an example https://www.gymbaroomteliza.com/treasure_bag/

  • Hi @phatoz

    If I get it right, you should provide the current date. Also, I’m sorry I forgot to remove the number “85”. It’s just a post ID for testing purpose. So, your code should be like this:

    <?php
    
    // date starting the week of loaded treasure bag
    $today = date('Ymd');
    $last_week = $today-6;
    $next_week = $today+6;
    
    $treasure_bag_date = get_sub_field('treasure_bag_date');
    ?>
    
    <tr <?php if( ($treasure_bag_date >= $last_week) && ($treasure_bag_date <= $next_week) ){ echo "id='active_week'"; }?>>

    I hope this helps 🙂

  • Yes, the $today was declared further up in the code so I didn’t include it in my snippet.

    I ended up using this code and it seems to work perfectly.

    $treasure_bag_date = get_sub_field('treasure_bag_date');  
    $today = date('Ymd');
    $next_week = date_create($treasure_bag_date);
    $next_week = date_add($next_week, date_interval_create_from_date_string('7 days'));
    $next_week = date_format($next_week, 'Ymd');?>
    			
    <?php // set #active_week to current week ?>
    <tr <?php if( ($today >= $treasure_bag_date) && ($today <= $next_week) ){ echo "id='active_week'"; }?>>
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Compare dates in rows against today’ is closed to new replies.