This functionality is better and even easier with GP Conditional Logic Dates.
Already own GP Conditional Logic? Check out this article!
This tutorial will demonstrate how to show/hide fields via Gravity Forms’ conditional logic based on the current time. This functionality can be used anywhere conditional logic is available.
We will be populating the current time into a field on the form via Gravity Forms dynamic population. We will then create conditional logic rules based on the value of this dynamically populated field.
Let’s get started!
Steps
- Add a Hidden field.
- Add snippet to populate the current time.
- Setup your time-based conditional logic.
1. Add a Hidden field
First, we’ll add a hidden field to our form. Let’s label this field “Current Time”. Now we’ll need to make a few configurations to this field so that it will be dynamically populated with the current time.
- Open the Current Time field and visit the Advanced tab
- Select the setting to “Allow field to be populated dynamically”
- Add a dynamic population parameter called “time”
2. Add snippet to populate the current time.
Now that your Current Time field is configured, we need to copy a small snippet to your theme’s functions.php (or any place you’re able to include custom code).
View this code snippet on GitHub.This code will dynamically populate the Current Time field with the time in a 24-hour decimal format. Here are some examples of how some times will be converted.
Time | Numeric Value |
---|---|
12:00am | 0.00 |
7:01am | 7.01 |
2:37pm | 14.37 |
11:59pm | 23.59 |
3. Setup your time-based conditional logic.
Our Current Time field will now be populated with the numeric time value each time the form loads. Let’s setup conditional logic for a field based on the value of our Current Time field.
In the demo, we have two Single Line Text fields. The first will be visible if the time is less than 12pm and the second will be visible if the current time is after 12pm.
There are an infinite ways this can be used! Don’t forget that this works anywhere Gravity Forms conditional logic is supported.
What questions do you still have?
Do you still have any questions about how to get this setup? We’d love to know what they are. Leave a comment below.