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.

Convert date entry on submission

  1. I've got a form that takes a start and end date for events. I know I can convert the output on the theme into strtime, but I have a lot of functions and logic that is based on it being in a timestamp and making it convert on entry would make life easier. I'm pretty sure I can do it on the pre_submission filter, but I'm struggling with the logic.

    Posted 7 years ago on Monday October 10, 2011 | Permalink
  2. Hi Andrew,

    Sounds like you're on the right track. Using the gform_pre_submission filter you can update the submitted the values in the $_POST.

    [php]
    $_POST[2] = date('u', strtotime($_POST[2]));
    Posted 7 years ago on Tuesday October 11, 2011 | Permalink
  3. OK, I think I'm almost there, but I'm not sure it's firing correctly. Here are the variables:
    Form ID: 1
    Start Date: field 3
    End Date: field 4

    and code:

    function ctny_datesubmit_fix ($form){
        //event start date field id is 3
        //event end date field ID is 4
        $raw_srt = $_POST['input_1_3'];
        $raw_end = $_POST['input_1_4'];
    
    	// convert my dates
    	$fix_srt = date('u', strtotime($raw_srt));
    	$fix_end = date('u', strtotime($raw_end));
    
    	$_POST['input_1_3'] = $fix_srt;
    	$_POST['input_1_4'] = $fix_end;
    }
    
    add_action('gform_pre_submission_1', 'ctny_datesubmit_fix');
    Posted 7 years ago on Wednesday October 12, 2011 | Permalink
  4. Looks good. Just drop the form ID from the $_POST keys.

    Example: input_1_3 becomes input_3

    Posted 7 years ago on Wednesday October 12, 2011 | Permalink
  5. OK. It works, but it won't post the UNIX time into the field on submission. As a test, I had the same input field (start date) post into a separate text field, and the converted time submits. Is there something with the date field that prevents anything other than a specifically formatted time to work?

    Posted 7 years ago on Wednesday October 12, 2011 | Permalink
  6. Found a workaround that'll do what I want. I made two admin-only fields (as text field) and did the time conversion and had those post to the custom fields instead of the "real" date picker.

    The code if anyone wants it

    // date corrections
    
    function ctny_datesubmit_fix ($form){
        //event start date field id is 3
        //event end date field ID is 4
        $raw_srt = $_POST['input_3'];
        $raw_end = $_POST['input_4'];
    
    	// convert my dates
    	$fix_srt = strtotime($raw_srt);
    	$fix_end = strtotime($raw_end);
    
    	$_POST['input_35'] = $fix_srt;
    	$_POST['input_36'] = $fix_end;
    }
    
    add_action('gform_pre_submission_1', 'ctny_datesubmit_fix');
    Posted 7 years ago on Wednesday October 12, 2011 | Permalink

This topic has been resolved and has been closed to new replies.