PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Combining fields

  1. I need to store a date and time as a single field in this format:

    2010-09-11 08:00:00

    Is there a way to collect this data using Gravity Forms' date and time fields and then concatenate them into one field? I see from this forum's "Localization of the Date Picker and Date Format" thread that changing the date slashes to dashes is a different issue, so please consider the "combining fields" aspect of this request.

    -Ken

    Posted 14 years ago on Wednesday April 14, 2010 | Permalink
  2. In order to combine date and time fields into one you would have to write custom PHP and interact with the available API hooks to concatenate them. So yes it is possible, but there isn't any built in functionality to do it. You would have to write PHP to do so.

    You would most likely want to add a hidden field that you then use to store the concatenated value. If you post a link to your form we can provide you with a code snippet to get you started.

    Posted 14 years ago on Wednesday April 14, 2010 | Permalink
  3. Thanks, Carl! The form is here:

    http://www.challengeblog.org/submit/

    I'm trying to store the data in the format expected by this plugin:

    http://wordpress.org/extend/plugins/the-events-calendar/

    That plugin has fields for both "startdate' and "enddate". I'm choosing to ask my users for only one date. Is there a way to store that data in two different custom fields?

    Posted 14 years ago on Wednesday April 14, 2010 | Permalink
  4. I haven't used The Events Calendar plugin so I am not familiar with how it stores data.

    So you want to combine these two fields, concatenate them and then store them as post meta I am assuming, correct?

    Do you know what the post meta key the plugin uses for the start and end date that you need to save?

    Posted 14 years ago on Thursday April 15, 2010 | Permalink
  5. Carl,

    Yup! I'm having no problem using Gravity Forms to collect and store all the other fields (great how your plugin is so versatile!). Here's everything The Events Calendar uses:

    _isEvent
    _EventStartDate
    _EventEndDate
    _EventVenue
    _EventCountry
    _EventAddress
    _EventCity
    _EventState
    _EventProvince
    _EventZip
    _EventCost
    _EventPhone

    Elsewhere, someone else has asked the author of TEC to make his plugin Gravity Forms-friendly.

    Posted 14 years ago on Thursday April 15, 2010 | Permalink
  6. Any update on this? I'm curious about the "code snippet" Carl mentioned to get started.

    Thanks!
    - Tim

    Posted 14 years ago on Friday April 23, 2010 | Permalink
  7. Unsurprisingly, I too am curious to see this code. :-)

    Posted 14 years ago on Thursday May 6, 2010 | Permalink
  8. Any update on this Carl? I'd really love to start using this form more.

    Posted 14 years ago on Monday May 10, 2010 | Permalink
  9. All right, here is the code snippet to combine the date and time fields, saving the result in another (hidden) field.
    Let me know if you have any issues with it.

    add_action("gform_pre_submission", "format_event_date");
    function format_event_date($form){
    
        //replace 5 with your actual form id
        if($form["id"] != 5)
            return;
    
        //replace 4 with your actual date field ID
        $date = $_POST["input_4"];
    
        //replace 5 with your actual time field ID
        $time = $_POST["input_5"];
    
        $hour = str_pad($time[0], 2, "0", STR_PAD_LEFT); //making sure hour has 2 digits
        $minute = str_pad($time[1], 2, "0", STR_PAD_LEFT); //making sure minutes has 2 digits
    
        //formatting date
        $raw_date = "{$date} {$hour}:{$minute}{$time[2]}";
        $formatted_date = gmdate("Y-m-d H:i:s", strtotime($raw_date));
    
        //replace 3 with the ID of the hidden field that will hold the formatted date
        $_POST["input_3"] = $formatted_date;
    
     }
    Posted 14 years ago on Tuesday May 11, 2010 | Permalink
  10. Thanks Alex. This works great in the functions.php? I had to modify some things to add an end date. Also had to make sure the holding field would create the proper new custom field. I'll post code once I have it refined.

    Posted 14 years ago on Wednesday May 12, 2010 | Permalink
  11. dxbmoves
    Member

    Him Tim, were you able to refine the code to add the correct custom field? I'm trying to use gravity forms with the Event Calendar plugin and facing the same "crisis" :)

    You help is appreciated.

    Posted 14 years ago on Wednesday June 2, 2010 | Permalink
  12. mmtrav
    Member

    Using the code in this linked post, I have managed to get my form submitting to my custom post type. Tested and everything's working.

    http://forum.gravityhelp.com/topic/add-a-form-to-create-a-new-custom-post-type#post-6525

    Then I used this code in this topic to put a field together for an event start date (trying to integrate with The Events Calendar plugin).

    However I do need one other thing that's beyond my knowledge -- how to get GF to file this eventstartdate in the post meta table, under the meta_key "_EventStartDate" ... I'm only guess that I need to have some sort of post_submission handler to achieve this? I will probably need a bit of Alex C's super coding to get the right idea..

    Help please guys?

    add_action("gform_post_submission", "add_EventStartDate");
    function add_EventStartDate {
    	}
    Posted 14 years ago on Tuesday August 3, 2010 | Permalink
  13. Using the update_post_meta WordPress function, you'll want to add a line that is something like this:

    add_action("gform_post_submission_5", "add_EventStartDate", 10, 2);
    function add_EventStartDate($entry, $form) {
        update_post_meta($entry['post_id'], "_EventStartDate", "your value");
    }
    Posted 14 years ago on Tuesday August 3, 2010 | Permalink
  14. mmtrav
    Member

    Hey Dave,

    Again, custom post success, but it looks like the event date isn't making it through correctly -- the post meta table is showing :00 for the new post.

    Here's my code. I'm really sorry, but this is like reading Arabic for me! I need help badly.

    /**
     * Sets our custom post types and taxonomies
     */
    
    function sp_gravity( $post_data, $form ) {
    	if( $form["id"] == '1' ) {
    		// Set our custom post type
    		$post_data["post_type"] = "sp_events";
    	}
    	return $post_data;
    }
    
    add_filter( 'gform_post_data', 'sp_gravity', 10, 2 );
    
    /**
     * Sets the event start date
     */
    
    add_action("gform_pre_submission", "format_event_date");
    function format_event_date($form){
    
        //replace 5 with your actual form id
        if($form["id"] != 1)
            return;
    
        //replace 4 with your actual date field ID
        $date = $_POST["input_1_11"];
    
        //replace 5 with your actual time field ID
        $time = $_POST["input_1_12"];
    
        $hour = str_pad($time[0], 2, "0", STR_PAD_LEFT); //making sure hour has 2 digits
        $minute = str_pad($time[1], 2, "0", STR_PAD_LEFT); //making sure minutes has 2 digits
    
        //formatting date
        $raw_date = "{$date} {$hour}:{$minute}{$time[2]}";
        $formatted_date = gmdate("Y-m-d H:i:s", strtotime($raw_date));
    
        //replace 3 with the ID of the hidden field that will hold the formatted date
        $_POST["input_1_13"] = $formatted_date;
    
     }
    
    add_action("gform_post_submission_1", "add_EventStartDate", 10, 2);
    function add_EventStartDate($entry, $form) {
        update_post_meta($entry['post_id'], "_EventStartDate", "$formatted_date");
    }

    Here's my form:
    http://www.brownsmart.com.au/venue-hire/event-submission-form/

    Posted 14 years ago on Tuesday August 3, 2010 | Permalink
  15. On this line:

    update_post_meta($entry['post_id'], "_EventStartDate", "$formatted_date");

    ...remove the quotes around the $formatted_date variable. Let me know if that does the trick.

    Posted 14 years ago on Tuesday August 3, 2010 | Permalink
  16. mmtrav
    Member

    Sorry David.. it looks like I still ended up with the same result. Have I bungled my code in another spot? That is quite likely because I don't know what i'm doing!

    Posted 14 years ago on Thursday August 5, 2010 | Permalink
  17. mmtrav
    Member

    I've checked the result getting through to the database..

    ::00

    Not sure if that represents a failed submission or not :(

    Posted 14 years ago on Thursday August 5, 2010 | Permalink
  18. If you want to send me FTP access to your WP install, I will take a look: david@ounceoftalent.com

    Posted 14 years ago on Thursday August 5, 2010 | Permalink
  19. With the new version of Gravity Forms you can now name fields which makes this hack much easier. However I'm running into an issue where the Start and End dates are not being passed to the Events UNLESS an editor goes in an manually unchecks the "All day event?" box and then publishes the post. Otherwise the date data is lost and it defaults to 1970. Any ideas how to fix this? I'm using this code in my functions file:

    add_action("gform_pre_submission", "format_event_date");
    function format_event_date($form){
    
        //replace 1 with your actual form id
        if($form["id"] != 4)
            return;
    
        //replace 4 with your actual date field ID
        $date = $_POST["eventday"];
    
        //replace with your actual time field ID
        $time = $_POST["starttime"];
        $timeend = $_POST["endtime"];
    
        $hour = str_pad($time[0], 2, "0", STR_PAD_LEFT); //making sure hour has 2 digits
        $minute = str_pad($time[1], 2, "0", STR_PAD_LEFT); //making sure minutes has 2 digits
    
        $hourend = str_pad($timeend[0], 2, "0", STR_PAD_LEFT); //making sure hour has 2 digits
        $minuteend = str_pad($timeend[1], 2, "0", STR_PAD_LEFT); //making sure minutes has 2 digits
    
        //formatting date
        $raw_date = "{$date} {$hour}:{$minute}{$time[2]}";
        $formatted_date = gmdate("Y-m-d H:i:s", strtotime($raw_date));
    
        $raw_dateend = "{$date} {$hourend}:{$minuteend}{$timeend[2]}";
        $formatted_dateend = gmdate("Y-m-d H:i:s", strtotime($raw_dateend));
    
        //replace with the IDs of the hidden fields that will hold the formatted dates
        $_POST["eventstartdate"] = $formatted_date;
        $_POST["eventenddate"] = $formatted_dateend;
    
     }
    Posted 14 years ago on Friday September 10, 2010 | Permalink
  20. @creativeslice I'm not sure because I haven't uploaded v1.4 yet, but it looks like you'll need to alter the last two lines from:

    $_POST["eventstartdate"] = $formatted_date;
    $_POST["eventenddate"] = $formatted_dateend;

    to...

    $_POST["_EventStartDate"] = $formatted_date;
    $_POST["_EventEndDate"] = $formatted_dateend;
    Posted 14 years ago on Thursday September 16, 2010 | Permalink
  21. I've got this code to work for Gravity Forms v1.3 and The Events Calendar. At the end I've also got a snippet to format the post_name from the post_title for the pretty URL. Also, my date and times are both in an array because i'm using the Date Field rather than the Date Picker in Gravity Forms.

    //GravityForms Modifications
    //change post type for Events to sp_events for Event Posting form
    add_filter("gform_post_data", "change_post_type", 10, 2);
    function change_post_type($post_data, $form){
    
        if($form["id"] == '5'){
            $post_data["post_type"] = "sp_events";
        }
        return $post_data;
    }
    
    //convert date and time to Events Calendar style for Event Posting form
    add_action("gform_pre_submission", "format_event_date");
    function format_event_date($form){
    
        //replace 5 with your actual form id
        if($form["id"] != 5)
        	return;
    
        //replace input_5 and input_6 with your actual date field IDs
        $startDate = $_POST["input_5"];
        $endDate = $_POST["input_6"];
    
        //replace input_23 and input_24 with your actual time field IDs
        $startTime = $_POST["input_23"];
        $endTime = $_POST["input_24"];
    
        $eventStartDate = $startDate[2] . "-" . $startDate[0] . "-" . $startDate[1] . " " . $startTime[0] . ":" . $startTime[1].$startTime[2];
    
        $eventEndDate = $endDate[2] . "-" . $endDate[0] . "-" . $endDate[1] . " " . $endTime[0] . ":" . $endTime[1].$endTime[2];
    
        $eventStartDate = strtotime($eventStartDate);
        $eventEndDate = strtotime($eventEndDate);
    
        //formatting date
        $formatted_start_date = strftime("%Y-%m-%d %H:%M:%S", $eventStartDate);
        $formatted_end_date = strftime("%Y-%m-%d %H:%M:%S", $eventEndDate);
    
        //replace with the IDs of the hidden fields that will hold the formatted dates
        $_POST["input_5"] = $formatted_start_date;
        $_POST["input_6"] = $formatted_end_date;
    }
    
    add_action("gform_post_submission", "add_EventDatesAndTimes", 10, 2);
    function add_EventDatesAndTimes($entry, $form) {
        update_post_meta($entry['post_id'], "_EventStartDate", $_POST["input_5"]);
        update_post_meta($entry['post_id'], "_EventEndDate", $_POST["input_6"]);
    }
    
    add_action("gform_pre_submission", "create_friendly_url");
    function create_friendly_url()
    {
    	 $phrase = $_POST['title'];
    	 $maxLength = 200;
        $result = strtolower($phrase);
    
        $result = preg_replace("/[^a-z0-9\s-]/", "", $result);
        $result = trim(preg_replace("/[\s-]+/", " ", $result));
        $result = trim(substr($result, 0, $maxLength));
        $result = preg_replace("/\s/", "-", $result);
    
        $_POST["postname"] = $formatted_dateend;
    }
    Posted 14 years ago on Thursday September 16, 2010 | Permalink
  22. sriganesh
    Member

    @brianfidler

    Thanks for the tutorial.
    It works perfectly.

    Btw, could you explain how to change the code if I was to use date picker in the gravity form.

    Thanks

    Posted 14 years ago on Sunday September 19, 2010 | Permalink
  23. Samantha
    Member

    Hello,

    I am interested in using the date picker as well. Thank you.

    Posted 14 years ago on Friday September 24, 2010 | Permalink
  24. Extrememuz
    Member

    The tut looks cool, I've got nearly every thing except something really simple.

    Where do the fields below go in Gravity Forms.

    _isEvent (make this “1″)
    _EventVenue
    _EventCost
    _EventStartDate (2010-05-20 09:00:00)
    _EventEndDate (2010-05-20 17:00:00)
    _EventPhone
    _EventCountry (United States)
    _EventState (AZ)
    _EventCity
    _EventProvince (???)
    _EventZip
    _EventShowMapLink
    _EventAllDay (make this “yes” if it’s a full day event)
    _EventAddress

    Thanks in advance Muz!

    Posted 14 years ago on Tuesday September 28, 2010 | Permalink
  25. skilfullycurled
    Member

    Hi everyone,

    I am trying to create a form to submit to a custom post type that has The Events Calendar metabox. I am able to get all of the variables into the post except the date and time. I am using a variation of alex's code which I have put in my theme's functions.php file.

    The form I am using has custom post fields of date and time type for the start and end dates and times respectively. The form also has two custom post fields of type hidden. I will work to get the form online so it can be inspected. Currently I am editing locally and using GF 1.5.

    Many thanks for your help the help of all those who attacked this problem before me!

    Benjamin

    http://pastebin.com/embed_js.php?i=wVwkeQjk

    Posted 13 years ago on Monday March 28, 2011 | Permalink
  26. skilfullycurled
    Member

    doh! could an admin please delete?

    lo siento.

    Posted 13 years ago on Monday March 28, 2011 | Permalink
  27. hi benjamin, could you please explain how you managene to get this working properly?

    thanks,

    Luc

    Posted 13 years ago on Friday April 8, 2011 | Permalink