Support

Account

Home Forums General Issues Date Picker Less than 30 days

Solving

Date Picker Less than 30 days

  • Evening All!

    Need a little bit of advice!

    I am using Date Picker as an ACF and on the front end I am trying to get it to calculate the date + 30 days with the result inputting a marker.

    I have sort of tried below, however no success:

    <?php 
    // get raw date
    $date = get_field('acf_date_field_name');
    
    if ($date < strtotime('-30 days')) { 
        echo "30 days";
    } else {
        echo "Its OK";
    }
    ?>

    Any help would be appreciated.

  • Hi @garywright

    Please keep in mind that strtotime() returns a timestamp instead of a formatted date. In this case, you need to convert the custom field date to a timestamp. It should be something like this:

    // Get the raw date and convert it to timestamp
    $date_timestanp = strtotime(get_field('acf_date_field_name', false, false));
    
    if ($date < strtotime('-30 days')) { 
        echo "30 days";
    } else {
        echo "Its OK";
    }

    I hope this helps 🙂

  • Hi James,

    Thank you for your reply.

    Does it matter what format I have in the ‘Save Format’ field of the ACF? I currently have dd/mm/yy.

    I have tried the script that you provided me and it always outputs as if it was less than 30 days (eg: echo “30 days”) even when the date is set 2 years ahead.

    If you want to see an example please look in the ‘Tax’ column here – http://rentals.londonmotorcycles.london/

    It’s the first time I have had to work with dates and calculating alerts etc so I am bit baffled by it all.

    Thanks for your help.

  • Hi @garywright

    I think I’ve made a mistake in my reply there. It should be like this:

    // Get the raw date and convert it to timestamp
    $date_timestanp = strtotime(get_field('acf_date_field_name', false, false));
    
    if ($date_timestanp < strtotime('-30 days')) { 
        echo "30 days";
    } else {
        echo "Its OK";
    }

    If you use ACF PRO version, the format doesn’t matter as long as you set the format to false in the get_field() function.

    In the other hand, if you use the free version, you need to use the right format before converting it to a timestamp. It should be something like this:

    // Get the raw date and convert it to timestamp
    $the_date = get_field('acf_date_field_name', false, false);
    $the_date = str_replace('/', '-', $the_date);
    $date_timestanp = strtotime($the_date);
    
    if ($date_timestanp < strtotime('-30 days')) { 
        echo "30 days";
    } else {
        echo "Its OK";
    }

    If that doesn’t work, could you please debug the data like this:

    echo $the_date;
    echo "<br />";
    echo $date_timestanp;

    Thanks 🙂

  • Hi James,

    Thank you for your reply.

    Unfortunately, still no luck with the code 🙁

    I have put the debug code in the page, and to confirm I am using the free version at the moment on this site.

    http://rentals.londonmotorcycles.london/

    Thank you for all of your help!

  • Hi @garywright

    I am sorry for the delayed response.

    I’ve just checked the page and it looks OK for me. Could you please tell me what’s wrong with it? Also, could you please add the debug code for each row?

    Could you also share the XML export file of your field group so I can test it out on my installation?

    Thanks!

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

The topic ‘Date Picker Less than 30 days’ is closed to new replies.