Date Comparitor


Occasionally we need to show or hide a section of a site based on a given date. Typically this happens when an event is running for a limited time, and the display for the event needs to be shut off when the event comes to an end. There are a few simple components needed for this task. At the very minimum, you need to get the current date, and you need to create an ACF field for the end date.

In ACF, set up a Date Picker field, and set the “Return Format” setting to “Ymd”. This will return the date in a YYYMMDD format, like 20190321.

In the code, write the following to grab the current date:

$today = date('Ymd');

After that, you’ll need to wrap the div in a PHP ‘if’ statement, checking to see if the current date is less than the end date, if it is then display the div.

if($today <= get_field('end_date'):
    // Code to display if today's date is earlier than end_date

One more thing to add as a precaution and a help for a user is to add an “emergency shutoff” in case a user wants to kill the events section early for whatever reason. Add a Button Group ACF field for the event, and simply have it toggle On or Off (set the default to Off).