Conditionally Hide Element 90 Days Prior to an Event Date

I’m looking to set a condition to hide an element until 90 days prior to an event date. I’m using FooEvents and the meta key is {{dc:post:meta key=“WooCommerceEventsDate”}}. This displays the countdown timer just fine, but I can’t figure out what condition to set to only show the countdown within 90 days.

I’ve tried {{dc:post:meta key=“WooCommerceEventsDate” type=“date” format=“Y-m-d” offset="-90 days"}} and it doesn’t work. Any help is appreciated.

Hey @RookDigital,

Thanks for writing in! We have responded your One Total Care already.

Cheers.

For anyone else who needs help with a similar problem, here is the fix via One Total Care.

  1. Create a new shortcode using PHP.

// =============================================================================

// Custom Shortcode to deduct 90 days to the event date

// =============================================================================

function themeco_remaining_days_shortcode($atts) {

// Extract attributes

$atts = shortcode_atts(array(

‘key’ => ‘WooCommerceEventsDate’, // Meta key for the event date

), $atts);

// Get the event date from post meta

$event_date = get_post_meta(get_the_ID(), $atts[‘key’], true);

// Check if the event date is valid

if (empty($event_date)) {

return ‘0’; // Return 0 if no event date is found

}

// Create DateTime objects for the current date and event date

$current_date = new DateTime(); // Current date

$event_date_obj = new DateTime($event_date); // Event date

// Calculate the difference between the two dates

$interval = $current_date->diff($event_date_obj);

// Get the number of days remaining

$days_remaining = $interval->days;

// Check if the event is in the past

if ($current_date > $event_date_obj) {

return ‘0’; // Return 0 if the event has already passed

}

// Return the number of remaining days as an integer

return $days_remaining;

}

add_shortcode(‘remaining_days’, ‘themeco_remaining_days_shortcode’);

// =============================================================================

This allows you to use [remaining_days key=“WooCommerceEventsDate”] in the condition like this:

[remaining_days key=“WooCommerceEventsDate”] > 0 AND
[remaining_days key=“WooCommerceEventsDate”] <= 90

Otherwise, with Twig enabled, you can use this condition:

{{ global.date({“format”:“Y-m-d”}) }} After {{ post.meta({“key”:“WooCommerceEventsDate”})|date_modify("-90 days")|date(“Y-m-d”) }}

Glad to know this has been resolved! Thanks for sharing.