Populate Date, One Year from Current Date

There is an newer and better version of this article here:

How to Populate and Modify Dates with Gravity Forms

Had a request to demonstrate how it would be possible to populate a date which was one year from the current date into a Gravity Forms field. I’m not sure how generically useful this snippet will be, but it’s a decent demonstration of how the gform_field_value hook works.

1. The Code

Copy and paste this code into your theme’s functions.php file.

<?php
/**
 ** Gravity Wiz // Gravity Forms // Populate Date One Year from Current Date
 * https://gravitywiz.com/populate-date-one-year-from-current-date/
 *
 */
add_filter('gform_field_value_year_from_date', function() {
	return gmdate( 'Y-m-d', strtotime( '+1 year' ) );
} );

Depending on your needs, you might like to update the format in which the date will be displayed. Refer to PHP’s date() function for a list of format variables. Replace the 'Y-m-d' with your desired format.

2. Set Your Field’s Dynamic Population Parameter

Open your field’s settings and click on the Advanced tab. Enable Allow field to be populated dynamically. This will reveal the “Parameter Name” input. Enter year_from_date as the parameter value.

Wondering why we use that parameter name? If you take a look at the filter we added in the code, it was gform_field_value_year_from_date. The second part of this filter is dynamic and can be set to whatever you would like to use for the dynamic population parameter. So if we wanted to use a parameter name of my_filter, the filter name would be gform_field_value_my_filter.

Questions?

Let us know in the comments!

Comments

  1. eben fabricius
    eben fabricius August 30, 2016 at 2:32 am

    Hi there

    Is there a way that i can default a date field to the 1st day of Next month?

    So if i navigate to my form can i have the value in the date field to default to the 1st of September 2016?

    Ciao

    Reply
  2. Steve
    Steve November 16, 2014 at 4:26 pm

    Hi David,

    I have a plugin that expires a user 1 year from reg date so this code is something I was looking for. I set a hidden field in my form to populate the expire date with your code.

    My date is coming back a Jan 1, 1970. Is there a time setting that is wrong somewhere? Is this a server setting or just the wrong code setting?

    Thanks in advance Steve

    Reply
  3. brian
    brian September 16, 2014 at 1:00 pm

    Hi Dave,

    thanks for sharing this. Just what I was looking for. Since I’m trying to connect Gravity Forms to Google Calendar via Zapier I have ‘Start Date & Time’ fields as well as admin-visible-only ‘End Time & Date’ fields that I want to auto-populate based on the ‘Start Date & Time’ values.

    Now I’ve used your code (slightly modified) and came up with this:

    But this adds ‘0 days’ to the submission date. And I want to modify it so that it adds “0 days” to the Start Date. Same with time. I want to have the ‘End Time’ field auto-populated, i.e. add “1 hour” to the ‘Start Time’ and thus get ‘End Time’.

    Any suggestions? Thanks Brian

    Reply
    1. David Smith
      David Smith Staff September 16, 2014 at 2:19 pm

      Hi Brian, your code was stripped out. You can share it by including a link to it (http://pastie.org). The basic premise is that you need to get the submitted start date and then overwrite the value in the $_POST for the end date with your modified date. I have a new snippet coming later this week that will add support for this.

  4. Tarequl
    Tarequl September 12, 2014 at 1:25 pm

    HI david, its really nice to get lot of gravity tips & trick from your site. You guys really done very well in all those hooks. I got a issue on populating data. I have two date field in my form in same page. First- “Production Date”, Second- “Expired Date” I only want to select “Production Date” on front-end. the expired date will automatically populate for one year later date. how its possible? plz help.

    Reply
  5. John Newvalley
    John Newvalley April 20, 2014 at 5:23 am

    Hello and Great tip!

    Do you know if it is possible to make another date-field +1 day of the first date-field if the field with departure time (HH/MM) is less than the field with arrival time (HH/MM).

    Use case : I have a site for restaurant/conference center that has guests who typically arrive at 18.00 and leaves around 02.00 the following night. If I try to import whis form-data to Google Calendar I get an error because the END-time is less than the START-time. It will however work with END-Date +1 of the START-Date, but not many who uses my form understands that the date has to be +1…

    If you have a suggestion on how to fix this it would be great!

    Reply
  6. Jeanine Kilgore
    Jeanine Kilgore April 16, 2014 at 1:05 pm

    The above worked perfect for a general +1 month! Very nice. But…

    I need to preload 2 date fields, one that is the FIRST day of the next month… one that is the LAST day of the next month… I’m a newbie to WP/GF.. I’ve dabbled in php coding a little but not allot.. would you possibly be able to help me at all…

    Reply
  7. Mitch
    Mitch February 24, 2014 at 2:55 am

    Hi David,

    How would I go about displaying today’s date + 1?

    So the date field would populate with that date. (which can’t be changed).

    Next line I would like to display as above, one year from today’s date + 1.

    Regards,

    Mitch

    Reply
  8. Scott
    Scott February 1, 2014 at 9:42 am

    Very cool, I am creating a reservation form for Birthday Club. Would it be possible to auto populate the users Birthday based on there Facebook profile?

    I will be running an add to people having birthdays within the week then lining to the form so would be great to input that automatically or ideally as much as the form as possible with their using there facebook profile?

    Thanks Scott

    Reply
    1. David Smith
      David Smith Staff February 1, 2014 at 4:36 pm

      Hi Scott, it looks like it would be possible (based on this) but I’m not sure on how straight forward it would be to implement. If you do something like this, I’d love to hear about it.

    2. Scott
      Scott February 1, 2014 at 4:42 pm

      Hey Dave , I actually found this and was able to auto populate the name and email but not the phone and b day..

      https://gravitywiz.com/populate-date-one-year-from-current-date/#comments

      I tried using this to add but still could not get working for some reason

      add_filter( 'janrain_gforms_profile_data', 'add_birthday_field' );

      function add_birthday_field( $fields ) { $fields['birthday'] = array( 'name' => 'Birthday', 'function' => 'birthday_field' ); return $fields; }

      function birthday_field( $profile ) { return $profile->merged_poco->birthday; }

  9. eve
    eve May 7, 2013 at 1:30 pm

    Thank you! this works perfectly. Now, can you tell me if it would be possible for this renew date will tell GF to change member role to non member so it locks them out of private area on expiry, as it would for a subscription type paypal transaction. I hesitate to use subscription as a transaction type as there is no way for it to not be recurring. This membership join formis is for one year only.

    Reply
  10. Phil
    Phil May 5, 2013 at 6:18 pm

    Have searched everywhere and this is so close to what I’m looking for…

    Is it possible to change from “YEAR from current date” to “MONTH from current date”, or WEEK or even 2xWEEKS?

    Reply
    1. David Smith
      David Smith Staff May 5, 2013 at 9:54 pm

      Hi Phil, here is an example of how to convert this to 1 month from the current date:

      http://pastie.org/7806595

      You’d just need to update the dynamic population parameter from “year_from_date” to “month_from_date” on the field settings. You can use this conversion as a basis to see how you might convert it to any other time difference. :)

  11. Robert
    Robert December 23, 2012 at 8:03 am

    Hi, let me first say; awesome website! Learned a lot of it, thank you for that!

    I am a bit of a newbie to WP/GF and I am trying to populate a dropdown field dynamically in a same manner as this blogpost. Ideally the dropdown would be populated like so;

    • Today
    • Tomorrow
    • Tuesday, 25 december
    • Wednesday, 26 december
    • etc.

    So it wouldn’t show +1 year but rather +1 day and displaying a different label. I figured the easy part (adding 1 day instead of 1 year) but I have no idea how to populate the dropdown with this script :x

    I tried this link (http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields) but that didn’t really work out. Could you point me in the right direction? :)

    Reply
    1. David Smith
      David Smith Staff December 30, 2012 at 3:32 pm

      Hi Robert, I can see that this would be useful. I assume that “Today”, “Tomorrow” etc would be the label of the selection but the value would be the actual date it represents? I’ve added this to my queue of snippets to write up. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Grab a bundle of free Gravity Forms plugins

Enter your email and receive our most popular free plugins and snippets, plus access to hundreds of others.

This field is for validation purposes and should be left unchanged.