How to Setup Time-based Conditional Logic with Gravity Forms

A simple, code-based solution for creating conditional logic rules based on the current time. Works anywhere Gravity Forms conditional logic is supported (i.e. fields, confirmations, notifications, etc).

Stop! There's a better way.

This snippet is available as a plugin with Gravity Perks, a suite of over 47 premium Gravity Forms plugins!

View Plugin Buy Gravity Perks

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

  1. Add a Hidden field.
  2. Add snippet to populate the current time.
  3. 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.

  1. Open the Current Time field and visit the Advanced tab
  2. Select the setting to “Allow field to be populated dynamically”
  3. Add a dynamic population parameter called “time”
time-based-conditional-logic-current-time-field

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).

add_filter( 'gform_field_value_time', function() {
	return date( 'G.i', current_time( 'timestamp' ) );
} );

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.

TimeNumeric Value
12:00am0.00
7:01am7.01
2:37pm14.37
11:59pm23.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.

time-based-conditional-logic-before-12Display this field before 12pm
time-based-conditional-logic-after-12Display this field 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.

Comments

  1. Andrei
    Andrei July 20, 2023 at 5:42 am

    How an I calculate the time between Current time field and the time of Submit? I am using GP Google Sheet and I need to set a column with the difference between time of submit and time from Current tine field, so I can track how much time a user needs for submitting the form.

    Reply
    1. David Smith
      David Smith Staff July 29, 2023 at 3:51 pm

      Hi Andrei, I think this would be possible by using our Populate Dates snippet.

      1. Create a Hidden field for “Start Time” and populate that with a full timestamp.
      2. Create a Hidden field for “End Time” and populate that with a full timestamp and set the override_on_submission flag so it will get updated to the actual submission time.
      3. Pass both fields to separate columns in Google Sheets.
      4. Add a third column in Google Sheets to calculate the difference between the two timestamps. Something like this: https://blog.golayer.io/google-sheets/add-time-in-google-sheets
  2. Elsa Rice
    Elsa Rice June 22, 2023 at 5:16 am

    I found the article on setting up time-based conditional logic in Gravity Forms extremely helpful! Time-sensitive forms can be a game-changer for many businesses, and this guide provides a clear and concise step-by-step process to implement such logic. The screenshots and explanations make it easy to follow along, even for those who are not familiar with coding. The ability to show or hide form fields based on specific time conditions opens up a world of possibilities for creating dynamic and personalized user experiences. Gravity Forms continues to impress with its flexibility and functionality, and this tutorial is a valuable resource for anyone looking to optimize their form interactions based on time.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 22, 2023 at 1:05 pm

      Hi Elsa,

      Thanks for the feedback. Glad to know the article was useful to you.

      Best,

  3. James
    James June 9, 2020 at 2:17 am

    Hi David how to make the the time filed dynamically changed one? the time filed is static after opening the page

    Reply
    1. Ryan Donovan
      Ryan Donovan June 9, 2020 at 10:11 am

      Hello James, Thanks for writing in. 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. What is being displayed as static? Thanks!

    2. James
      James June 10, 2020 at 12:48 am

      Thank you Rayan for the reply in my case the time is populating while opening the page. but it is not changing , means the time is stable which populated while opening the page. so its not working with the logic that set for the time after that .

      what i expect is the time should change dynamically like a clock. is this function available ?

    3. James
      James June 10, 2020 at 10:38 am

      Thank you Ryan for providing another solution i tried this one by adding to an HTML filed and the time is appearing. but it not changing like a clock unless we refresh the page.

      My aim is to create a filed with current time which is dynamically changing like a clock and assign conditional logic’s on another fields depends this time filed

      thanks a lot for all your support

    4. James
      James June 10, 2020 at 12:14 pm

      similar to this

      function display_c(){ var refresh=1000; // Refresh rate in milli seconds mytime=setTimeout(‘display_ct()’,refresh) } function display_ct() { var x = new Date() var x1= x.getHours( )+ “:” + x.getMinutes() + “:” + x.getSeconds(); document.getElementById(‘ct’).innerHTML = x1; display_c(); }

  4. Mike H
    Mike H February 18, 2020 at 2:47 pm

    Could this work with the submit button, or the entire form? I’m looking for a way to effectively schedule the form based on dynamic dates provided by custom fields.

    Thanks

    Reply
    1. Scott Ryer
      Scott Ryer Staff February 18, 2020 at 5:11 pm

      Hi Mike, yes this would work with the submit button. If you want to set the conditional logic based on custom field data, I suggest looking into Populate Anything to populate those values into your form, which you can then use with conditional logic.

  5. Tim Burkart
    Tim Burkart January 28, 2020 at 4:34 pm

    Can I use this for setting time limits for completing forms? Save & continue will not be an option. For example I would like to offer timed quizzes on my website. Will your solution work for this?

    Reply
    1. Scott Ryer
      Scott Ryer Staff February 20, 2020 at 3:43 pm

      This snippet doesn’t support relative times. The conditional logic is limited to comparing the current time to a static time, such as 12pm.

  6. Andy
    Andy May 17, 2019 at 10:44 am

    Hi, I can’t seem to get the time to populate dynamically. I’ve tried pasting the code into functions.php and Code Snippets, but my date remains empty.

    I can’t use the Conditional Logic Dates perk as it doesn’t work with Advanced Conditional Logic Pro which I’m trying to use for some slightly more complex logic to determine days and times.

    Reply
    1. Andy
      Andy May 17, 2019 at 11:37 am

      Hmmm, I tested lots of things changing themes and deactivating plugins etc. Then I rolled the database back to where I was before I started fiddling… and the field is populating as it should. Weird, but working fine :)

  7. Don Bassler
    Don Bassler October 23, 2018 at 10:48 am

    I need a solution where a notification is sent to a particular email address between the hours of 5-7pm. Can this solution do this?

    Reply
    1. David Smith
      David Smith Staff October 23, 2018 at 11:51 am

      Would this be per submitted entry? If so, we have an early access perk available to Gravity Perks customers called GF Notification Scheduler which sounds like a good fit.

  8. Diego
    Diego July 16, 2018 at 3:15 pm

    Hi, I have one question:

    When using “current time”, the WordPress Time is considered?

    I’m asking because my WordPress and server times are different… and maybe it can uses the user time too…

    Reply
  9. Guy Haines
    Guy Haines March 12, 2018 at 1:49 pm

    What about ensuring the user completed the form in a certain amount of time (ie 4 days) and what considerations need to be made for if they clicked “Save and Continue” ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

How to Setup Time-based Conditional Logic with Gravity Forms

This field is for validation purposes and should be left unchanged.