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.
Hi Can you dynamically populate the date field from url parameters? I have url parameters for day, month and year, which I’d like to use to populate the date field. Thanks Ian
Hi Ian,
You can use the Graivity Forms Dynamic population setting to populate the field with the date value passed via the query parameter.
Best,
Hi. This looks very interesting to apply. However when playing around with the snippet, I noticed that if a modifier is used which changes the transition period from AM to PM (or vice versa), it displays incorrectly. For example, using a time of 12:15pm with a modifier of -1 hour, it gives me a result of 11:15pm and not the expected 11:15AM. Is this a known issue?
Hi Peter,
I was able to recreate the issue. It appears the snippet works well if the Time Field format is set to 24 hours. I will pass this over to our developers to see if support for 12 hours Time fields can be added. We’ll update the comment when we have more information.
Best,
Hi Peter,
This issue has been fixed. Please download the latest version of the code to get it working for you.
Best,
Hello! Please, tell me what wrong: new GW_Populate_Date( array( ‘form_id’ => 10, ‘source_field_id’ => 2, ‘target_field_id’ => 6, ‘modifier’ => ‘first Tuesday’, ‘format’ => ‘d.m.Y’, ) );
I tried to get a first Tuesday of month for source field, but I get first Tuesday for chosen week (not month)
When “first Tuesday of” I always get 01.01.1970
Hi,
This doesn’t appear to be currently supported. If you have an active Gravity Perks Pro license, you can get in touch via our support form and we can get our developers to add support for this.
Best,