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)
View this code snippet on GitHub.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
View this code snippet on GitHub.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
View this code snippet on GitHub.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
View this code snippet on GitHub.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.
Please note: When using this approach the date is modified and populated into the target field on form submission. The source field will not be populated immediately after the date has been selected. If you’d like to commission this enhancement, get in touch.
gf_hidden
class to the “CSS Class Name” setting. This special class will hide the field from view while still allowing the populated value to be captured.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.
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!