Skip Navigation

[Resolved] conditional logic by date

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 0 reply, has 1 voice.

Last updated by Minesh 4 months ago.

Assisted by: Minesh.

Author
Posts
#2796245
Screenshot 2025-02-11 at 11.02.37 AM.png

Hi

I'd like to have text show up in a view/content template based on a date.

So:

[If today's date is before Feb 28 at 9am then SHOW] text here [/conditional]
[If today's date is between Feb 28 at 9am and March 15 at 9am then SHOW] text here [/conditional]
[If today's date is after March 1 at 9am then SHOW] text [/conditional]

Can you help with the shortcode for this? This will be in a classic view. here is a sample of what I have now based on other conditions, but I need to add the date element as well.

#2796286

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to just clarify that do you have any custom date field and you want to compare the custom date field value against date value like "before Feb 28 at 9am" or you just want to compare the value like "before Feb 28 at 9am" with current "today's" date?

#2796352

Hi - thanks,
We do not want to use a custom date field (b/c this would apply to ALL POSTS),

We would like to compare a specific value like "before Feb 28 at 9am" with current "today's" date

#2796493

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can add the following custom shortcode to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_shortcode('compare_current_date_time','func_compare_current_date_time');
function func_compare_current_date_time($atts,$content){
  
  if(isset($atts['before']) and !empty($atts['before'])){
    	if(time() < strtotime($atts['before']))
    	     return 1;
        else
          	 return 0;
  }
  
   if(isset($atts['between']) and !empty($atts['between'])){
     	$dates = explode("|",$atts['between']);
        if ((time() >= $dates[0] && time() <= $dates[1]))
          	return 1;
     	else
          	return 0;
   }
  
 	if(isset($atts['after']) and !empty($atts['after'])){
      	if(time() > strtotime($atts['after']))
       		 return 1;
     	else
          	return 0;
  	}

}

Once you add and activate the ccode snippet, please regiter the custom shortcode name compare_current_date_time at:
=> go to the Toolset -> Settings page, click the Front-end Content tab and add your shortcode name in the Third-party shortcode arguments section.

More info:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/#using-the-custom-shortcode

Now, you can add the following conditionoal statements to check the current date againsts provided date values of before/after/between.

Compare crrent date Before:

[wpv-conditional if="( '[compare_current_date_time before='2025-02-28 9:00:00']' eq '1' )"]
  //// current date is less than (before) the provided date
[/wpv-conditional]

Compare crrent date Between:

[wpv-conditional if="('[compare_current_date_time between='2025-02-28 9:00:00|2025-03-15 9:00:00']' eq '1' )"]
  
   ////current date is beweeen provided dates (start-date|end-date):2025-02-28 9:00:00|2025-03-15 9:00:00 
  
[/wpv-conditional]

Compare crrent date After:

[wpv-conditional if="( '[compare_current_date_time after='2025-03-01 00:00:00']' eq '1' )"]
  //// current date is greater than (after) the provided date
[/wpv-conditional]

More info:
=> https://toolset.com/documentation/programmer-reference/displaying-custom-functions-with-views-and-using-them-in-conditional-logic/#using-custom-functions-with-views-conditional-logic
=> https://toolset.com/documentation/legacy-features/views-plugin/conditional-html-output-in-views/

#2796548

AMAZING! Thank you so much.
I've followed your instructions but cannot get it to work..

Here are the steps I took.

hidden link

#2796734

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2797902

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing admin access details but when I try to login using the admin access details you shared I see the error:
"Error: The username toolset is not registered on this site. If you are unsure of your username, try your email address instead."

Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#2798040

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - Can you please check now.

I've adjusted the code added to "Custom Code" section as given under:

add_shortcode('compare_current_date_time','func_compare_current_date_time');
function func_compare_current_date_time($atts,$content){
   
  if(isset($atts['before']) and !empty($atts['before'])){
        if(strtotime($atts['before']) < time() )
             return 1;
        else
             return 0;
  }
   
   if(isset($atts['between']) and !empty($atts['between'])){
        $dates = explode("|",$atts['between']);
        if (( time() >= strtotime($dates[0])   &&   time() <= strtotime($dates[1]) ))
            return 1;
        else
            return 0;
   }
   
    if(isset($atts['after']) and !empty($atts['after'])){
        if(strtotime($atts['after']) > time() )
             return 1;
        else
            return 0;
    }
}

I've used the following conditional statements:


   [wpv-conditional if="( '[compare_current_date_time before='2025-02-14 9:00:00']' eq '1' )" debug='false']
  //// current date is less than (before) the provided date
[/wpv-conditional]
     
   [wpv-conditional if="('[compare_current_date_time between='2025-02-23 9:00:00|2025-03-15 9:00:00']' eq '1' )"]
   
   ////current date is beweeen provided dates (start-date|end-date):2025-02-28 9:00:00|2025-03-15 9:00:00 
   
[/wpv-conditional]
   
   [wpv-conditional if="( '[compare_current_date_time after='2025-03-01 00:00:00']' eq '1' )"]
  //// current date is greater than (after) the provided date
[/wpv-conditional]

Can you please confirm its working as expected now.

#2798102

AMAZING SUPPORT. Yes, this is great!!

THANK YOU!

#2798119

Hi again - The only thing is that I think it's using Universal time instead of local time... is there a way to adjust that?

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-conditional-logic-by-date-adjust-timezone/

#2798187

Christopher Amirian
Supporter

Languages: English (English )

Hi,

This is Christopher's answer back as Minesh is on vacation at the moment.

The Date/Time fields are stored as UNIX timestamp in Toolset that is why there is no timezone feature to be able to work with.

Thanks.

#2798464

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery.

I've split the ticket with your new question and I'll follow up there:
- https://toolset.com/forums/topic/split-conditional-logic-by-date-adjust-timezone/

I kindly ask you to mark resolve this ticket with different message.