Skip Navigation

[Resolved] Gravity Forms Date Field Compatibility Issue

This thread is resolved. Here is a description of the problem and solution.

Problem: I am using a form created in Gravity Forms to create new posts. I would like to capture a date using a GF date field and store it in a Types date field, but the formats are incompatible.

Solution: Add some custom code to functions.php or to a new custom code snippet in Toolset > Settings > Custom code:

// convert gravity forms date field to types date format
add_filter("gform_post_data", 'set_my_gravity_date', 10, 3);
function set_my_gravity_date($post_data, $form, $entry ) {
  $form_id          = 12345;
  $types_field_slug = 'types-field-slug';
  $gf_field_id      = 67890;
   
  // you should not edit below this line
   
  //check if this is the right form
  if ( isset($form['id']) && $form['id'] == $form_id ) {
  // add unix timestamp for date field in meta_input
  $post_data['meta_input']['wpcf-' . $types_field_slug] = strtotime(rgar($entry, $gf_field_id));
  }
  return $post_data;
}

You should change 12345 to match the Gravity Form ID. You can find that in the form editor. You should change types-field-slug to match the slug of your custom date field. You can find that by editing the custom field in wp-admin. Then you should replace 67890 with the numeric ID of the Gravity Forms date field. You can find that in the GF form editor when you hover over the field. Then use the Types field shortcode to display the field with whatever format you want on the frontend of the site.

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Edward Barker 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1200144

Hi,

So this is possibly opening up an old Toolset war wound. I'm having the same problem as discussed here from 2013 .....

https://toolset.com/forums/topic/gravity-forms-pass-date-value-to-date-format-custom-fields-unix-format/

Is there a comprehensive answer now available to match up the two date fields between Toolset and Gravity forms?

For reference I'm using the field slug of feedback-date-of-care-review and would like the format in DD/MM/YYYY if possible.

Kind Regards
Ed

#1200242

Hi, yes it's possible with a bit of custom code. The format you want to see on the front-end of the site is really irrelevant when storing the date information. What we want to do is translate Gravity Forms date format into Toolset format for storage. That value gets stored in the database. Then to display the date on the front-end of the site, you'll use a Types field shortcode. You can set whatever format you would like in that shortcode.

First, you need to add some custom code to functions.php or to a new custom code snippet in Toolset > Settings > Custom code:

// convert gravity forms date field to types date format
add_filter("gform_post_data", 'set_my_gravity_date', 10, 3);
function set_my_gravity_date($post_data, $form, $entry ) {
  $form_id          = 12345;
  $types_field_slug = 'types-field-slug';
  $gf_field_id      = 67890;
  
  // you should not edit below this line
  
  //check if this is the right form
  if ( isset($form['id']) && $form['id'] == $form_id ) {
  // add unix timestamp for date field in meta_input
  $post_data['meta_input']['wpcf-' . $types_field_slug] = strtotime(rgar($entry, $gf_field_id));
  }
  return $post_data;
}

You should change 12345 to match the Gravity Form ID. You can find that in the form editor. You should change types-field-slug to match the slug of your custom date field. You can find that by editing the custom field in wp-admin. Then you should replace 67890 with the numeric ID of the Gravity Forms date field. You can find that in the GF form editor when you hover over the field.

Documentation for displaying the date field here: https://toolset.com/documentation/customizing-sites-using-php/functions/
Click the orange "+More" button under the date field to see examples with different formats.

#1200289

This fix is perfect, thank you very much.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.