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!
You’re probably familiar with Gravity Forms conditional logic. That awesome feature that allows you to show or hide fields depending a set of custom conditions. One feature that isn’t yet support is date-based conditions. Or is it?
This method will allow you to show or hide any field based on a date. You can configure it to show/hide if that date has passed, has not passed or if you are currently on that date.
This tutorial is based on the following example. You are selling tickets to a conference. Tickets sold a week before the conference are $10. Tickets sold within a week of the conference are $15. Let’s dig in!
1. The Code
Copy and paste the above snippet into your theme’s functions.php file. This code enables you to populate a field on your form with the current date and time in the form of a UNIX timestamp.
It isn’t super important that you know what a UNIX timestamp is (the number of seconds since January 1, 1970) but it is important to understand that a timestamp is an integer. No weird date formatting just a really big number. For example, this post was published on March 1, 2013. The time stamp for this date is 1362117600.
Gravity Forms conditional logic can’t work with formatted dates but by converting the date into a number we can let Gravity Forms handle the rest.
2. Setting Up Time Stamp Field
Our timestamp snippet doesn’t do much on it’s own. We need to tell it which field to populate with the timestamp. Go ahead and open any form to which you would like to add this functionality and follow these steps:
- Add a new Hidden field and label it “Timestamp”
- Click on the “Advanced” tab to view the field’s advanced settings.
- Check the checkbox labeled “Allow Field to be Populated Dynamically”.
- Enabling the “Allow Field to be Populated Dynamically” option will reveal the “Parameter Name” input. Add this value
timestamp
.
Awesome! Now this field is configured to be dynamically populated with the current timestamp any time the form is viewed.

3. Setting Up Conditional Field
Now it’s time for the fun part!
- Create another field that you would like to which you would like to apply the date-based conditional logic.
- Click on the “Advanced” tab to access the field’s advanced settings.
- Check the “Enable Conditional Logic” checkbox.
- Select the “Timestamp” field from the first drop down.
- Select “less than” or “greater than” from the operator drop down depending on your intent (more details below).
- Enter the timestamp for the date this condition is based on (more details below).
For our working example, we will have two product fields. One with the early bird pricing (any time a week before the event) and another for regular pricing (any time within a week of the event).
The event is on March 18, 2013. We want the pricing to be dependent on a date one week before the event, so March 8, 2013 is our “conditional date”. The timestamp for this date (set to midnight) is 1362700800. Based on that conditional date, here is how each field’s conditional logic would be configured:


Should I use “less than”, “greater than”, or “is”?
If the field should show/hide if the current date is before the conditional date, you will want to use the “less than” operator.
If the field should show/hide if the current date is after the conditional date, you will want to use the “greater than” operator. You’ll note in the example, the regular pricing has an extra “is” clause. Without this, there would be one second at midnight on the conditional date that neither pricing field would display.
If the field should show/hide between two dates, you will need to create two conditions and use the “greater than” for the starting date condition and “less than” for the ending date condition.
How do I get a timestamp for my conditional date?
The conditional date will also need to be in the UNIX timestamp format. Use this online UNIX timestamp converter to convert the date you want to base your show/hide condition on to a timestamp. Once you have your timestamp, enter it into the last input on the condition row.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
I’m curious, why not return the WordPress function current_time()?
Sorry, I meant, current_datetime().
Hi Joel,
Current_datetime() returns the current Date and Time as an Object but Gravity Forms conditional logic requires the Date and Time to be in a UNIX timestamp format, which is why time() is being used.
Best,
Is there a way to have Gravity Forms reload the timestamp when someone clicks the Next button? I’d like to be able to compare when they landed on the form to when they move on to the next page of the form (they are supposed to be watching a video first). Right now it appears to pick up the timestamp once when the form is first rendered and then use that value indefinitely.
Hello, We currently do not have a solution for this, but if you are a Gravity Perks license holder, we may be able to come up with a solution for you. You can contact us through our support channel found here. Have a wonderful day. 😀
Hi David, its possible get timestamp with milliseconds ?
Hello, yes it can! Here is another converter that shows the unix time in milliseconds https://currentmillis.com/.
David
Can this be used to set conditional logic on a notification (send if timestamp is after a certain date)?
Hello Scot, Yes this can be used in confirmation and notification conditional logic. Make sure to convert the time and date properly using this calculator. Your best bet would be to use Gravity Perks Conditional Logic perk to make this much easier.
This is great! Curious about the logic though. If the function is called “gwiz_populate_timestamp”, why does using “timestamp” work as the parameter name in the hidden field. Surely this should also be “gwiz_populate_timestamp”?
Hi Christopher,
gwiz_populate_timestamp
is the function name. We map thetimestamp
parameter to this function via theadd_filter
call. 🙂Hi How can I disable certain dates in date picker in gravity forms? I used this code in a html tag in my form but it didn’t work out Please help me this is the code:
gform.addFilter( ‘gform_datepicker_options_pre_init’, function( optionsObj, formId, fieldId ) { if ( formId == 12 && fieldId == 14 ) { var disabledDays = [’06/15/2014′, ’07/01/2014′, ’07/15/2014′]; optionsObj.beforeShowDay = function(date) { var checkdate = jQuery.datepicker.formatDate(‘mm/dd/yy’, date); return [disabledDays.indexOf(checkdate) == -1]; }; } return optionsObj; });
I mean make some certain dates non-selectable in the date picker
This plugin will help: https://gravitywiz.com/documentation/gravity-forms-limit-dates/
Hello I want to make date conditional fields based on odd and even days, for example when the date is odd show sth, but when the date is even show sth else How can I do that? Thanks in advance
Hm, not sure if this is possible… would this be based on the current date or a user selected date?
Hi you know, I mean for the ticket reserving when the customer choose odd days(for example the first and the third and fifth day of the month and so on) show specified departure time and if customer choose even days(the second and the forth day of the month and so on)the form show another time options. I mean every other days
I gave this some thought and I can’t think of any ready-made solutions for this one.
Hi David,
I have ordered the plugin Perks so I can make a calculation with the time stamp. The only thing i have to know is what calculation i have to use!
A have a taxi service to the airport and if my clients make a booking for service in 24 hour I want them to pay 10 euro extra.
So today it is august 28th 18.22 and if they order the taxi for 30th august 18.23 thay pay the normal price and if they order the taxi for 30th 18.22 and earlier they have to pay 10 euro extra.
I already have a hidden timestamp and thats working, the only thing i need to know is the calculation.
The website is https://www.luchthavenexpress.nl
Hi Albert, we’ll be happy to help you via the support form.
Hi David,
What i need is I need to charge 10€ when the booking is within 24 hours. Can you tell me if there is a solution?
Like: today it is august 28 time is 04:40. If my clients book at august 29 04.39 or earlier they pay 10€ more and if they book august 29 04.40 or later then there is no extra charge.
Can you please help me?
Thanks, Albert
Hi Albert, you say within 24 hours. Is it within 24 hours of the event/product that they are booking?
is there a solution for dropdown options to base on current time?
example.
current time = 1.15pm available choices 12.00 PM 12.30 PM 1.00 PM 1.30 PM 2.00 PM 2.15 PM 2.30 PM
dropdown will only show choices for
1.30 PM 2.00 PM 2.15 PM 2.30 PM
Hi Nigel, we actually do have a snippet for this here:
https://gist.github.com/spivurno/edbe7189a152f37772a5a65aadee6b22
We provide support for our snippets to Gravity Perks customers. If you have any questions, just drop us a line via the support form. :)
Hi David,
the snippet worked after configuration.. however. it does not take into consideration the current date and affected every date. is there a chance you put current date into consideration?
there can be a field for datepicker, as long as it selects today’s date, the snippet will turn on?>
Hello David,
I would like to change the next day date after 12PM everyday If User clicked on a meal under DIET PLANS, user is asked to choose Start Date. Start date must be current date + 4 days at the earliest (provided order and payment is being made before 12 noon. If order and payment is after 12 noon, start date must be current date + 5). So if user is making the order on May 29 (Monday) before 12 noon, earliest start date is June 02 (Friday). User cannot click on June 01 (Thursday) as that is current date + 3 days only.
This is the site link
http://healthyfood.studioscue1.online/
You can configure two different Date fields with GF Limit Dates to limit the dates that can be selected. One field would be based on if the time is before noon. The other would be if the time is after noon. Then use the “Current Time” special conditional logic option provided by GF Conditional Logic Dates to show the correct field based on the current time.
Hi David I have a issue and may be you can guide me. I place a code sniper in to functions.php and follow your instractions. I have form that require registration for specific event which has daily rate and weekly rate So I created 2 dropdown product fields one is for early registration the other for registrartion tha has no discount. ( I test it with changing my PC time to acomodate both time positions) Long story to short when form opens up none of the product fields shown. So my question What am I doing wrong.
Thank you I appreciate your imput LA
Here’s a good place to start. :)
https://gravitywiz.com/documentation/snippet-troubleshooting/
I fond problem . I did nor realize that Parameter Name: should be all lawcase I had Timestamp( TYPO) and needed to be timestamp as soon as I crrected it start working
Thank you for your replu
Hi I want to be able to send a notification to a user that submitted a form based on the above idea. So when a particular date is reached (ie that a timestamp is greater than – and that a particular field is not complete) that a notification is automatically sent to the user to remind them to supply information for the blank field. I am using Gravity Forms with Gravity View front end form entries. So an ‘automatic’ notification after a specific date. I do not require the form to be viewed just that a notification is automatically sent – is this possible using the above principle. If not is there an alternative? Kind Regards Phil Macaulay
Hi Phil, we have a solution available by request available to Gravity Perks license holders. It will allow you to configure the notification to send based on a variety of time-based conditions screenshot. If you’d like to take it for a spin, pick up a copy of Gravity Perks and send us a request via support.
Thx for this snippet I can successfully get the number of days between 2 dates. I would like to use the number returned to calculate a price based on that number. So for example, – if number is 2, price is $15 – if number is 3, price is $18 – if number is 4, price is $19 Etc…
The issue i am having is tryng to calculate these prices, as you can see there is no regular pattern, i have 30 different prices based on the number returned!
Any help appreciated
GP Conditional Pricing will make that a breeze.