September 20, 2022: Added support for modifying dates by a number of weekdays via the "weekday" and "weekdays" keywords.
March 23, 2022: Fixed issue where a modifier value of 0 would be ignored rather than used.
March 9, 2022: Improved support for Time fields using the "24 hour" Time Format.
September 27, 2021: Fixed an issue where modified dates were over-written on form submission.
September 21, 2021: Fixed issue GPPA could not rely on dates populated with this snippet due to an order-of-events issue.
September 7, 2021: Fixed a compatibility issue between the snippet and newer versions of GPPA.
February 26, 2021: Added the latest version of
strtotime.js
.December 29, 2020: Fixed an issue where populating to a non-date field without specifying a format would fail.
October 15, 2020: Migrated snippet to the Snippet Library.
January 16, 2020: Fixed notices generated by use of deprecated
create_function()
function.February 11, 2015: Fixed issue where date format was mangled when using the source_field_id parameter and a non-US date format.
February 7, 2015: Added support for "min_date" parameter. Allows you to ensure a minimum calculated date when modifying a user-specified date.
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 46+ essential add-ons for Gravity Forms with support you can count on.
I keep getting invalid date
I have the config as
new GW_Populate_Date( array( ‘form_id’ => 1, ‘source_field_id’ => 5, ‘target_field_id’ => 6, ‘modifier’ => ‘+7 days’ ) );
Hi JS,
The configuration seems correct. This will require digging into your code to see what could be going on. If you have a Gravity Perks license, can you get in touch with us via our support form?
Cheers,
is it compatible with the date merge tags snippet?
other than that i dont have anything else custom
Hi JS,
They should work together the same as with all of our Perks and Snippets. That being said, a specific configuration on your site or form could be conflicting in this case. That will require some digging. You can try deactivating one of the Snippets and see if that fixes the issue.
Best,
Great and details article.
But what if I want to display the current date and increase it by 2. For example, today’s June 01, 2022, what if I want to display June 03, 2022 instead.
I also notice when I put [eid tag=”{today}” /] it gives me the current date but I’m not able to display the date increase by 2. How can I do this, please?
Ps: I’m using eid tag because I’m using elementor
Hi Pedro,
The {today} merge tag isn’t a functionality of the snippet used in this tutorial. It’s part of Gravity Forms Core. However, using the snippet you can populate a Date field with the current date increased by 2 but I am not sure that will work with a Merge tag.
You’ll need some custom code to get a merge tag to display the current date increased by 2 days. If you have an active Gravity Perks license, you can get in touch with us via our support form so we can look into this further.
Best,
So forgive me for asking likely a simple question. I don’t need to use this everywhere, but I do need to use it on two different forms with different form, source and targets for each.
I can’t call GW_Populate_Date more than once in a snippet, and this does not seem to work with the usual GW_Populate_Date_4 (form id) notation.
Suggestions on using this specifically for two different forms?
Not need to apologize! You can instantiate the class multiple times and specify the form ID inside of each class.
Thank you Dario for clarifying. I think a workaround could be using a hidden field to convert the 24hour time to a 12 hour time format. However I am not sure how to do this. Can you advise?
Thanks!
Hi Will,
You could use Copy Cat perk to copy the values from one field to another.
Best,
Hi. Interesting solution. But it isn’t about copying values. It is the format. For example if i have “15:00” to convert, it would not convert to 3:00 pm. What I understand of Copy Cat is that it copies values exactly from the source field. Am I correct?
Hi Will,
Yes, Copy Cat will copy the value as it is. If you have coding skills you could filter the value to get a calculated hour based on the input field. Otherwise, I would suggest reaching out if you have a Gravity Perks license via the support form so we can assist you further with this.
Cheers,
This is great! I am able to add e.g. 1 hour to a time field (12 hour clock), but cannot get it to work if the source field is a 24 hour clock. Can you help?
Hi Will,
This is a known issue. The snippet currently only works with Time fields set to 12 Hours. We don’t have an ETA about when this functionality will be added.
We hope to have a solution soon 🧙♂️
Best,
Hi I have a date field in Jalali (= persian, farsi, shamsi) format. It works fine. I have an another date field and want to populate it’s value based on that Jalali date field in Gregorian format.
I try this snippet and it works well.
But It can not convert the date, Just copy the first one and paste it in second one.
Hi Mo,
We’ll need more information about your setup and configuration to be able to assist you. If you have a Gravity Forms License, you can get in touch with us via our support form with an export of your form and any additional information so we can assist you further.
Best,
Is it possible to make the modifier dynamic by linking it to a gravity form field? For example, if the user selects 5 the auto-populated date would be 5 days in the future?
Hi,
From what I understand, this is currently not supported.
If you have an active Gravity Perks License, you can get in touch with us via our support form with your account email address and we’ll be happy to dig into this further.
Best,
I have multiple forms – I use the same field id but different form id’s. I would like to configure the snippet so it can work with multiple forms, is there a way to indicate several form id’s in the new gw-populate-date function?
Nevermind, I found the answer. => https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Thanks!
Hi Edward,
Awesome, glad you were able to find your answer in that documentation.
Best,
Hi, Is it possible to apply this for a particular step of Gravity Flow? In the start form, a field called number of days is filled out. I have to add that field to a field of type date in a particular step of Gravity Flow. How can I do this?
If the step is a User Input step, then yes. The snippet will only trigger when a user is interacting with the form. If the Gravity Flow step is of another type, that isn’t supported.
I’m having a strange issue. When I see filed created by this snippet on a form it looks ok BUT when it’s actually submitted/stored, an entry has a strange fixed date and time that was not set for every single entry, no matter when it is submitted at later times.
Example of what I’m using:
setlocale( LC_TIME, ‘sl_SI.UTF-8’ );
new GW_Populate_Date( array( ‘form_id’ => 1, ‘target_field_id’ => 23, ‘modifier’ => ‘+28 hours’, ‘format’ => ‘l, j. F 0B %k:i’, ‘enable_i18n’ => true, ) );
It’s even stranger, because when I tested it, it worked OK and the next user submission after that on a live page as well, but on all the next ones the above happens.
What could be the issue here?
Thanks!
Hi Jernej,
This will be a bit difficult to determine the actual cause of the issue without extensive troubleshooting. If you have a Gravity Perks license, you can get in touch with us via our support form and we’ll be happy to dig into this further.
Best,
Can I use this to get a long date format for a date picker field? I would like to get a mergetag showing January 1, 2021 instead of 1-1-2021. Can I do this directly on the same field or do I need to add this into a new field? Thanks
Hi Mauricio,
Populating a Date field with the long date format won’t work, because it won’t allow you to submit the Date in that format. Instead of using the Date field, you can populate the Date in the long format into a Single Line Text field and this should work for you. Refer to the PHP date() function for a full list of available date formatting options.
Best,
Dear Team,
How can I populate +1 year to the end date of that month from the current date?
Hi Raja,
I’m not sure I understand your question correctly, but what I am getting is that you want to populate a Date field with a date 1 year from the current date. If so then you can use a snippet configuration with the modifier set to +1 year similar to what’s below, to do that;
new GW_Populate_Date( array( 'form_id' => 39, 'target_field_id' => 6, 'modifier' => '+1 year' ) );
Best,
Hello,
Thanks for this feature.
I have one specific case. Maybe you can help me with that ?
I want to change the modifier parameter according to the value of a specific field.
If the user select the value “3”, the modifier parameter will be “+3days”, else if the user select the other value the modifier parameter will change accordingly.
How can it be done ? Thank you
I think I found the answer up there…
Hi Paul,
Glad you were able to figure it out.
Best,
Can this be used on multiple forms? Do you just copy/paste the array for each form you want it used on?
Hi Justin,
You can use this on multiple forms by creating multiple instances of the snippet’s Class. Below is the link to the documentation on how to do that;
https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Best,
Hi, I have this installed and it works to set a default date two days in the future, but it does not block out the dates up to then. How do I set it so that the user cannot select any date less than two days from now?
I restricted the dates with this script:
http://snippi.com/s/jtrss4a
Hi Mark,
This can be accomplished with GF Limit Dates.
This may be just what we need. I work for made-to-order sauna manufacturer. During this Covid-19 pandemic, demand for saunas surged 300%. Thusly … they’d love to create the following functionality
Probably not possible with gravitywiz, but I’ll ask… * Automatically Put this date on the staffs’ calendar. Drag’n’drop on an actual calendar would be really amazing since dates need to be moved. Moving a date updates the production date field.
Hi Dan,
This is a specific use case and I am not really sure you can achieve everything using Gravity Forms. That said, you could definitely use the snippet in this tutorial to populate another date field with the production date. You can then use GP Populate Anything to populate a field with the category ID of the selected Product, which will be used to determine if the Product is Suana Build or not.
Regarding your request to have a drag and drop feature within a calendar, unfortunately, we do not have a solution to this. You may want to speak to a developer to see if they can assist with ideas or solutions to this. You can hire a developer from Codeable.io
Best,
Could we take the date from the submission of the entry and add one day onto that? I am running into an issue where a form was submitted at 8:19 am CST on 12/23/20 but the field that is storing the next business day is listed as 12/23/20 when it should be 12/24/20, so I think I am running into a timezone issue. So, if I could just use the submission entry date and add a weekday onto that things should be fine.
Hi Michael,
Currently specifying the time zone in the snippet isn’t supported. It matches your site’s time zone, which is set within the WordPress settings. You’ll find it in the General settings.
Scott,
Thanks for the reply.
If this is the case why could it be that that on a form was submitted at 8:19 am CST on 12/23/20 but the field that is storing the next business day was listed as 12/23/20 when it should be 12/24/20?
Hi Michael,
If the server time zone is set correctly, it sounds like there’s something else at play. If you have a Gravity Perks license, drop us a line and we’ll be happy to dig into this for you.
Thank you for the information. Another question – Is it possible to specify the timezone in this snippet?
Hey Michael, this is an interesting question. What is your intention behind changing the timzeone? Let us know so we could help you out with one.
Ryan,
A form on my website was submitted at 8:19 am CST on 12/23/20 but the field that is storing the next business day is listed as 12/23/20 when it should be 12/24/20. Another entry was submitted at 9:27 am CST on 12/23/20 and the field that is storing the next business day is correct. So, I think I am running into a timezone issue. That was my intention for specifying a timezone to make sure the next business day’s date is correct.
Thank you for the help! Michael
Hello!
Thank you for this! Would it be possible to calculate +1 Business Day from the Current date? For example if the form was submitted Monday-Thursday the date calculated date would be the next day. However if the form was submitted Friday-Sunday the date calculated would be the following Monday.
Hello Michael, You could actually pass the +1 weekdays modifier to allow for the days only count Monday – Friday.
Hi Ryan, trying to create +2 day Monday to Friday only, no weekends. ‘modifier’ => ‘+1 weekdays’, not working, can you help? So the idea is, populate a date +2 days, Monday to Friday only, so Thursday should be Monday, and Friday should be Tuesday. Thanks.
Hi Pavel,
Since you already have a ticket with the same issue, we’ll continue assisting you via the ticket, so that we can keep track of this properly.
Best,
thanks,this is great! Can I ask: does this account for leapyears? E.g if you want to populate a date 1year ahead in a leapyear, how would you use this?thanks!!
Hello Daniel, since this is using the PHP date function, the PHP’s DateTime class does support leap years.😀