Skip Navigation

[Resolved] Form Advanced Conditional Display Based on Age and Birthdate Field

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that includes a custom date field. That date field is used to save a birthdate. I would like to create a conditional group that displays some content in the Form if the birthdate is less than 18 years ago.

Solution: Toolset's date fields store Unix timestamps that represent dates. These are simple numbers. If you want to compare that date to a date in the past, you need to create a Unix timestamp for that date in the past. Then you can compare those two timestamp values like comparing any other numbers. The following custom shortcode can be used to generate a Unix timestamp from the text string "18 years ago":

add_shortcode( 'tssupp-strtotime', 'tssupp_strtotime');
function tssupp_strtotime($atts = [])
{
  $atts = shortcode_atts([
    'string' => '',
  ], $atts);
  $time = strtotime($atts['string']);
  return $time;
}

Register tssupp-strtotime in Toolset > Settings > Front-end Content > Functions Inside Conditional Evaluations. Then in the basic Form builder you can implement the shortcode in a conditional like so:

( '$(cliente-data-de-nascimento)' gt '[tssupp-strtotime string="18 years ago"]' )

Or in expert mode:

[cred_show_group if="( '$(cliente-data-de-nascimento)' gt '[tssupp-strtotime string="18 years ago"]' )" mode="fade-slide"]
Age is less than 18
[/cred_show_group]

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-conditional-display-engine/

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by celso-jP 5 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1386127

Tell us what you are trying to do?
I have a date field for Date of Birth and I want to show a Conditional Group only if the Age is lower than 18.
I tried the following code but it did not work.

info of the date field:
<input type="text" id="cred_form_100_1-textfield-4-1574090099-90"
name="wpcf-cliente-data-de-nascimento[display-only]"
value="" class="js-wpt-date form-control wpt-form-textfield form-textfield textfield hasDatepicker"
style="" readonly="readonly"
title="Selecionar data"
placeholder="" data-wpt-type="textfield"
data-wpt-id="cred_form_100_1_cred_form_100_1-textfield-4-1574090099"
data-wpt-name="wpcf-cliente-data-de-nascimento[display-only]">

code used on the manual area of condition:
if ( int ( (DATE(TODAY(),'Ym')-DATE([wpcf-cliente-data-de-nascimento],’Ym’))/100 ) < 18 )

What should be the code to accomplish this goal?
Thanks in advance.

Is there any documentation that you are following?
I am trying to check the Documentation area of Toolset site.

Is there a similar example that we can see?
Nope.

What is the link to your site?
hidden link

#1386427

Hi, I have a custom shortcode that can help here. Please add this custom code to your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom Code:

add_shortcode( 'tssupp-strtotime', 'tssupp_strtotime');
function tssupp_strtotime($atts = [])
{
  $atts = shortcode_atts([
    'string' => '',
  ], $atts);
  $time = strtotime($atts['string']);
  return $time;
}

Next, go to Toolset > Settings > Front-end Content and add tssupp-strtotime to the Functions inside conditional evaluations section. This will register the shortcode so you can use it in a conditional.

Finally, use the following syntax to set up the cred conditional:

[cred_show_group if="( '$(cliente-data-de-nascimento)' gt '[tssupp-strtotime string="18 years ago"]' )" mode="fade-slide"]
Age is less than 18
[/cred_show_group]

Please let me know if this is not working as expected and I can take a closer look.

#1386489

Thanks Christian for your help. Actually I was not using EXPERT MODE so I Just had to open 'add condition manually' and copied just this part of your solution:

( '$(cliente-data-de-nascimento)' gt '[tssupp-strtotime string="18 years ago"]' )

and it worked like a charm.

My issue is resolved now. Thank you!