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 populating a date 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 you’re specific need has not already been covered.
- At a minimum, you will want to set the
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 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.
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 strtotime() function for a full list of available date modification commands. Some examples include:
+1 day
,+2 weeks
,+1 week 2 days
,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.
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 32+ essential add-ons for Gravity Forms with support you can count on.
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.
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.😀