September 20, 2022: Added support for modifying dates by a number of weekdays via the "weekday" and "weekdays" keywords.
Way back when (in 2012), we wrote an article on how to populate a date, one year from the current date. We’ve rewritten this snippet to be more flexible, easier to configure, and added support for creating a Gravity Forms auto populate date setup based on a user-specified date.
What does that mean? It means you can let the user select a date via a Date field and then you can add a year to that date and populate it into another field.
How is that useful? Maybe you’re selling a year-long membership where the user can define when they would like their membership to begin. They select the date they would like their membership to begin (via a Gravity Forms Date field) and this snippet would allow you to populate the date their membership would expire (a year later).
Why would you need the expiration date? Well, first of all… you ask a lot of questions. But seriously, it’d be nice if you could let the user know when their membership expires in the confirmation message or a notification email. If you’re super pro, you could even automatically cancel the membership after the expiration date (you’d need some extra code to do this).
This is just one example of how this snippet might be used. I’m sure there are a million more and I want to hear about them. Make sure you share your usage ideas in the comments.
Getting Started
Check requirements
- Make sure you have Gravity Forms installed and activated.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Make sure you have Gravity Forms installed and activated.
Install the snippet
- Copy and paste the entire snippet into your theme’s functions.php file.
Configure the snippet
- At a minimum, you will want to set the
form_id
parameter to the ID of your form and thetarget_field_id
to the ID of the field in which the date should be populated. If you’d like to modify the date to be populated, set themodifier
parameter with a value like+1 year
or+7 days
. - There are many, many more configuration options available for this snippet. Continue reading if your specific need has not already been covered.
- At a minimum, you will want to set the
Gravity Forms Auto Populate Date Usage Examples
Populate Current Date (Today)
new GW_Populate_Date( array(
'form_id' => 39,
'target_field_id' => 4,
) );
Populate a Date field (or any other text-based field type) with the current date by replacing the form_id
parameter with your Gravity Forms form ID and the target_field_id
with the ID of the field you want to be populated.
Populate Date One Day from Today
new GW_Populate_Date( array(
'form_id' => 39,
'target_field_id' => 6,
'modifier' => '+1 day'
) );
Set the modifier
parameter to modify the current date. You can add or subtract time and in some really neat ways. Additional usage instructions below.
Populate Current Time (Now)
new GW_Populate_Date( array(
'form_id' => 39,
'target_field_id' => 4,
) );
Populate a Time field with the current time by replacing the form_id
parameter with your Gravity Forms form ID and the target_field_id
with the ID of the field you want to be populated.
Populate Time One Hour from Now
new GW_Populate_Date( array(
'form_id' => 39,
'target_field_id' => 6,
'modifier' => '+1 hour'
) );
Set the modifier
parameter to modify the current time. You can add or subtract time and in some really neat ways. Additional usage instructions below.
Populate Date One Year from Today with a Custom Date Format
new GW_Populate_Date( array(
'form_id' => 39,
'target_field_id' => 7,
'modifier' => '+1 year',
'format' => 'F j, Y' // i.e. March 10, 2015
) );
Set the format
parameter if you’d like to populate the date in a specific format. In this example, the date would be output like so: “September 20, 2014”. The default format is Y-m-d
which would look like this: “2014-09-20”. Additional usage instructions below
Populate Date One Year from User-specified Date
new GW_Populate_Date( array(
'form_id' => 39,
'target_field_id' => 3,
'source_field_id' => 1,
'modifier' => '+1 year'
) );
Set the source_field_id
parameter to the ID of the field from which the submitted value should be modified and used to populate the target field. The source field will generally be a Date field where the user can select a date.
Modify Date by Field Value
new GW_Populate_Date( array(
'form_id' => 1895,
'target_field_id' => 2,
'source_field_id' => 1,
'modifier' => array(
'type' => 'field',
'inputId' => 3,
'modifier' => '+{0} days',
),
) );
The modifier
parameter accepts an array which is used to modify the date by field value. Set the inputId
parameter to the ID of the field that should modify the target field value. The modifier field should be a Number field for user input. The modifier
inside the array behaves the same as other use cases, except {0} will be replaced with the field value.
modifier
parameter also works with Time fields.Force Minimum Date
new GW_Populate_Date( array(
'form_id' => 828,
'target_field_id' => 2,
'modifier' => '+1 year',
'min_date' => '07/16/2022',
) );
Set the min_date
parameter to force a minimum date until the modifier exceeds it.
Format Date in Specific Language
setlocale( LC_TIME, 'es_ES' );
new GW_Populate_Date( array(
'form_id' => 144,
'target_field_id' => 2,
'modifier' => '+7 days',
'format' => '%A',
'enable_i18n' => true,
) );
Use setlocale to specify the locale and set the enable_i18n
parameter to true. The target field will output the date using the chosen language.
Parameters
Here is a full list of the available parameters and additional information on how each can be configured.
form_id (integer) (required)
The ID of the form.
target_field_id (integer) (required)
The ID of the field that you want to populate with the current date.
-
The format in which the date should be populated into the target field. Default value:
Y-m-d
(i.e. “2014-09-20”). Refer to the PHP date() function for a full list of available date formatting options.If your target field is a Date field, the populated date will be formatted using whatever date format is selected in the field settings.
source_field_id (integer) (optional)
The ID of the field whose submitted value will be modified and populated into the target field.
-
A time specific string that will be used to modify the date of the target field.
Refer to the PHP Relative Formats doc for a full list of available date modification commands. Some examples include:
+1 hour
,+1 day
,+2 weeks
,+5 weekdays
,next Thursday
, andlast Monday
. min_date (integer|string) (optional)
A timestamp or date string (i.e. ’01/01/2016′) that will enforce a minimum calculated date when modifying a user-specified date.
This is particularly useful when calculating a renewal date when the user is renewing in advance of their subscription end date.
enable_i18n (boolean) (optional)
Format date and time according to locale. Locale must be set using setlocale.
override_on_submission (boolean) (optional)
Set to true to repopulate data on submission, overriding the pre-rendered value. This is useful if want the date to be based on the time of submission rather than the time the form loads.
How’d we do?
If you use it and like it, let us know. We’d love to hear the different ways you found this code useful!
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.
Hello,
I have added this code and it works great. I looked through the comments and could not find how do you add more than one ID and Field ID number?
Do I keep repeating the code or can I just add it to the end of this code new GW_Populate_Date( array( ‘form_id’ => 3, ‘target_field_id’ => 158, ) );
I tried all kinds of ways by: ‘form_id’ => 3, 13, ‘form_id’ => 3, ‘form_id’ => 13, repeating the complete code up above and none of this worked.
So how do add another ID and Field ID without having to repeat the code over and over and over.
Thank you so much!!
P.S. What plugin did you use to add “Notify me of followup comments via e-mail” down below. We would like to add this to our website. We are currently using GeneratePress. Sorry I know this has nothing to do with the topic but I thought it could not hurt to ask.
Hi Brandon,
You can instantiate the class multiple times to use the snippet with multiple forms. More info here:
https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
We’re using this plugin for comment subscriptions.
Is there a way to save a date entry from Gravity Forms as a timestamp rather than as a date/time format? A user selects a date , or a date/time , as normal using the date picker , but then the value is saved as a timestamp? Thanks
Hey Chris, Yes, Gravity Forms includes the timestamp for when the form was submitted. It is an available form field when exporting entries, etc. It is called Entry Date. Let us know if you need anything else.
Thanks for the reply. I’m not looking for the ‘entry date’ as a timestamp, but rather a date field that a user selects a date, or date/time. There are several formats for saving a date in GF, but none of them are TIMESTAMP. Just wondering if there is a way to automate a field populating with the timestamp format of a date that a user selects?
Hi Chris, As you rightly said GF date fields do not support TimeStamp, but with some custom JS snippet you could populate a single-line text field with the timestamp of a date selected by a user. If you have a Gravity Perks License, you can drop us a line via the support form so we can assist you with this.
Best,
Perfect, thanks .
Hi,
I need one single Date field that is populated by today’s date + a user specified number of weeks. I have a dropdown menu where the user selects their “due date”, and the options they have is 1, 2, 3 or 4 weeks.
So the date field would be populated with todays date + 1, 2, 3, or 4 weeks – depending on what the user selects from the dropdown.
I’ve tried using the Date Time Calculator, but can’t seem to get it to work.
I can use the snippet on this page to get 4 different date fields populated with the right dates, and use conditional logic to display the correct date, but I need 1 single date field that is the final output for the “due date”.
Hope that makes sense.
Any ideas how I can do this?
Cheers Ian
Hi Ian,
This looks like something that can be done with some customization to the snippet. If youβre a Gravity Perks customer, can you please drop us a message via the support form and we’ll be happy to assist you. Remember to include an export file of the form.
Best,
Hi,
I am trying to work with GP Date Time Calculator to :
How can I make that work?
Hello Tobias, Excellent question. You will need to create multiple instances of the same snippet. More details here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/.π
Hi,
i did initially try with the date class snippet here, but wanted to just get it to work natively with your date time plugin.
I do have this partially working, but not with the desired output. Her ethe example I have (after the function, but seems I can get rid of that due to having the paid plugin)
Here is what I have :
Working:
new GW_Populate_Date( array( ‘form_id’ => 1, ‘target_field_id’ => 29, ‘modifier’ => ‘+1 day’ ) );
Not working as intended $difference = $entry[’52:value’];
new GW_Populate_Date( array( ‘form_id’ => 1, ‘target_field_id’ => 53, ‘modifier’ => ‘$difference’ ) );
So that difference field just contains : Age Label value 1 -1 year 2 -2 years
So I am trying to dynamically deduct this field from today’s date, but all I get back in the field ID 53 is some date in 1970
I tried with and without quotes around $difference.
Thanks for your help
Hello Tobias, thanks for the reply. As you stated, you have our paid plugin could you please reach out to our support queue with this question so we could take a closer look into your setup? Go ahead log into your account here and submit a ticket. π
Is there a way to “translate” displayed date into a local language? My WP site is already using my language for dates but this snippet is displaying month and weekdays in English.
Any solution to this?
Thanks!
Hello Jernej, are you currently using a translation plugin? Is the plugin displaying the owords month and weekends in english or the actual month and weekend dates? Thanks!
Hello there, after contacting a developer regarding this, we actually do have an advanced version of this snippet available to Gravity Perk License holders. If you have a license could you please reach out to our support team and we would be more than happy to help out with this one.π
Hi there, thanks for this useful article, I need to populate the target date field according to another date field (source) and a custom value which is called “nom” and holds the number of months (an integer)
target date field = source date field + nom months
for example: source date : 3/9/2020 nom : 10
the target must be as below: target date: 1/9/2021
the nom field ID is 14, and the other field numbers are mentioned in the following snippet code :
Configuration
$value = $entry[14]; new GW_Populate_Date( array( ‘form_id’ => 7, ‘target_field_id’ => 17, ‘source_field_id’ => 16, ‘modifier’ => ‘+$value months’ ) );
I retrieved the nom value using $entry[field_ID], but it still doesn’t work.
would you help me please?
In addition, isn’t it possible to calculate the target_date by AJAX at the time the user enters the nom value ?
best regards bro.
Hello Amin, Are you currently getting an error or is the nom value not displaying at all? Let us know and we can better assist!
Hi Ryan, how are you man? If I check the “required” option for the field and don’t select a date when submitting the form, it shows the common error that the field doesn’t have a value. But when I uncheck “required” option of the field, it doesn’t show an error, but after submitting the form, in the Inbox the NOM value is stored as “01/01/1970”. would you help me please?
Regards.
Hello Amin, Thanks for the description. We have a perk that is a better fit for this. Itβs called Date Time Calculator and it would allow you to calculate/populate the userβs months with a calculation like {Date:1} – {today}. As for your issue, regarding the nom value being stored while unrequired, we would need to take a deeper dive into your form’s configuration to better assist with this one. We would be happy to dig into this via our support form. Thank you! π
Hi again, Isn’t it possible to help me here like other applicants please? because, to be honest, I’m using the free version of this module and therefore I will not be supported :( I’m so sorry <3
I believe the create_function() used in this snippet is depreciated.
Thanks for the extension, it works great. In the array I give at ‘modifer’ => ‘+18 months’, it’s okay, but i want a random date between 18,19 or 20 months or random between 547 – 608 days from the source field. What I need change? Thx for help & support.
Error message Uncaught Error: Class ‘GW_Populate_Date’ not found in ….. What could i be doing wrong?
This will help: https://gravitywiz.com/documentation/snippet-troubleshooting/
Hey, stumbled across this while trying to figure out how to convert the date format for admin. I couldn’t see a way to add a certain format to the admin dropdown, and I had read the only way to get a new date format was to use 3 fields with gform_pre_submission_filter to concatenate those fields together.
For the most part the snippet here works amazing! However, I get two entries each time a form is submitted (one with the current date and one with the newly formatted date).
I’m sure there’s probably a much simpler snippet I need, or just part of this snippet to get the outcome I’m going for.
Is there a way to only modify the date format entered by a user into a different format for admin within the same input field? Totally fine to use a hidden field for the newly formatted date as well if needed.
Thanks so much!
Mike
We don’t have a solution for this, Mike. Our recommendation would simply be to have two fields, one for the admin and one for the user.
Hi guys, is it possible to use this in a time field at all please?
Thanks
Ben
As in could I let the user select a time of day like 3pm and add and hour to another field to make 4pm please? This would be most useful.
This does not currently support Time fields. You could certainly do this if you were populating the time into a Single Line Text field though.
Hi there, thanks for this… is it possible to add a modified date into an html block using a shortcode?
Thanks,
Ben.
If you use this snippet to populate a field with the modified date, you could display that modified date in an HTML field with live merge tags (a feature of our Gravity Forms Populate Anything plugin).
Hey there :)
Was using this snippet successfully for a couple years, and now suddenly the date is being rejected by the form because it requires (mm/dd/yyyy) and what is being populated is (Month dd,yyyy) All the snippet parts in the functions file refer to m-d-y and even if i change it to MdY or any variation, the result remains the same. How the heck do I force the form field to populate the digits rather than formal name, and without the comma?
Hugely appreciate any help!
Hi David – the snippet is being rejected when I try to add it to functions.php
Is there a workaround or paid version of the snippet?
There are a few options: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
Hi David! Thanks for your quick response. I’m not sure why, but the snippet still isn’t working. When I attempt to use it, an error message pops up saying…
“Class ‘GW_Populate_Date’ not found”….followed by strings
oh my — silly mistake David, sorry! [slaps forehead] — you’ve been great! I have not. lol
Can this be used with some logic? I want to take a date and add a month, 3 months or a year based on the value of another field.
We have an advanced version of this snippet that supports field-based modifiers. In this way, you could create a field that had a numeric value for the number of months that should be added (1, 3, 12) and the date would be modified based on the user’s selection. The advanced version is available to Gravity Perks customers via support.
Can this be used to populate a field with the user’s age? I have a GF with an input for the user’s DOB and another field for their age. I’d like for the age field to be dynamically populated once the user enter’s their DOB.
Is that possible?
Hi Chris, we have an early-access perk that is a better fit for this. It’s called Date Time Calculator and it would allow you to calculate/populate the user’s age with a merge tag like {Date:1:age}. If you’re a Gravity Perks customer, we’ll be happy to send you a copy of this plugin via support.
Thanks David! Awesome product BTW!
My pleasure, Chris! Glad you like it. ?
Curious if this can be applied to labels – specifically those in lists. Say I wanted to get the state a person lived in over the last three years. I wouldnt want to change my form from 2017, 2016 and 2015 to be 2018, 2017 and 2016 when the new year rolls around. It would make more sense to have the list field column headers update based on the current year?
This isn’t a great fit for this use-case. I would recommend setting the List column labels via the gform_pre_render filter and PHP’s date() function to get the desired years.
The advanced version of this snippet seems not working in newest version Gravity Forms 2.4.2, F12 showing error like this:
“gravityforms.min.js?ver=2.4.2:1 Uncaught RangeError: Maximum call stack size exceeded at GFCalc.replaceFieldTags (gravityforms.min.js?ver=2.4.2:1) at GFCalc.runCalc (gravityforms.min.js?ver=2.4.2:1) at GFCalc.runCalcs (gravityforms.min.js?ver=2.4.2:1) at HTMLDocument. (gravityforms.min.js?ver=2.4.2:1) at HTMLDocument.dispatch (jquery.js?ver=1.12.4:3) at HTMLDocument.r.handle (jquery.js?ver=1.12.4:3) at Object.trigger (jquery.js?ver=1.12.4:3) at Object.a.event.trigger (jquery-migrate.min.js?ver=1.4.1:2) at HTMLDocument. (jquery.js?ver=1.12.4:3) at Function.each (jquery.js?ver=1.12.4:2)”
I have a similar error message.
My setup: Gravity Forms Version 2.4.5 Theme: GeneratePress Version: 2.2.1 WordPress 5.0.3
Hi – Seems like a really nice snippet. Need your help to better figure out the following aspects:
The earlier version of this snippet, “Populate date, one year from current date” supposedly works for all forms by default without specifying a form Id. Is there a way to make this new snippet work for all forms by default, if all of them are supposed to use the current date + 1 Year time for expiry date (user specified start/end date is not required in my case). Or, should I go with the previous version of this snippet, if it is not doable with this new version.
Quote: “Why would you need the expiration date? Itβd be nice if you could let the user know when their membership expires in the confirmation message or a notification email.”
Does the above statement imply that this notification email regarding the expiration date can only be sent at the time of filling the form? Or is there a way to send these notification emails to the user closer to the expiry date as well, for example, for a 1-year subscription, sending these notification emails as reminders 30 days, 15 days, 1 day before expiry date, and one after the expiry date during the grace period (to make payment for renewal of subscription)?
One way, I can think of is creating separate administrative visibility fields with current date + 335 days for reminder 1, current date + 350 days for reminder 2, current date + 364 days for reminder 3, and current date + 380 days for the final grace period reminder. And then, setting up separate notification email reminders for each of these fields to be automatically sent to user, when current date matches these respective reminder dates (by enabling conditional logic in form notification settings) with the snippet: “send manual notifications with Gravity Forms”. However, I can’t find the date field in the conditional logic fields in Form Settings –> Notifications. Is this doable and does this make sense? Or is there another way to accomplish this objective of sending expiry reminder emails to users?
Need a bit more clarity here. For instance, if the user chooses to renew his annual subscription 10 days before the expiry date, in that case, his new subscription expiry date should be set accordingly (+ 1 year and 10 days from the date of renewal payment) as his previous subscription has not yet expired. How to accomplish this using this min_date parameter or otherwise?
Request you to kindly help me out here with your invaluable guidance. Many thanks in advance,
Best, Ambuj
Hi David – Any update on the above queries. Kindly let me know if this is doable with the available free snippets, and if not, which Gravity perk(s) do I need to purchase to accomplish the above objectives fully. Thanks a lot for your help.
Best, Ambuj
Hi Dave, I have added this Snippet “How to Populate and Modify Dates” to my themes functions.php file and it’s working perfectly!!! It looks like I need to take this one step further. I am going to need to set the number of days added to each of my date fields based on one of the drop down values in the form. Can you or someone on your team provide me with some sample code how this would be done? Would that code be placed at the end of the php file or does it need to go to another place in the program. I’m not a PHP programmer so sorry if I have to ask some very basic questions.
Thanks again!! Jon
Hi Jon, that is not possible with this version of the snippet. We have an advanced version available to Gravity Perks license holders that you can be requested via support.