Created
September 1, 2015 12:35
-
-
Save cliffordp/9662ce248bbb89d4d19b to your computer and use it in GitHub Desktop.
Combine several Gravity Forms form fields into a hidden "timestamp" form field using PHP's strtotime()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // USA timezones available at http://php.net/manual/en/timezones.america.php#114172 --> links to http://stackoverflow.com/questions/4989209/list-of-us-time-zones-for-php-to-use | |
| /* | |
| * change Gravity Forms date fields to timestamps, and do other formatting stuff as needed | |
| * inspired by https://gist.github.com/norcross/2277175 or https://gist.github.com/BronsonQuick/2834114 | |
| * https://www.gravityhelp.com/documentation/article/gform_pre_submission/ | |
| */ | |
| // http://php.net/manual/en/function.date.php | |
| // Make two fields of type 'Hidden' (NOT visibility -> Admin-only) on your GF form | |
| // and post to the custom fields from Gravity Forms | |
| // instead of the "real" date picker. | |
| // NOTE: change the input IDs to match the ones on your form | |
| function cp_gf_tour_datetime_fixes($form) { | |
| // get form entry data | |
| $raw_start_date = $_POST['input_2']; // field ID #, e.g. 2015-07-07 | |
| $raw_start_time = $_POST['input_4']; // e.g. 09:30 | |
| $raw_timezone = $_POST['input_10']; // e.g. America/Denver | |
| $raw_duration = $_POST['input_5']; // e.g. 90 (for 1.5 hours) | |
| // build what we want to see | |
| $start_timestamp = strtotime($raw_start_date . $raw_start_time . $raw_timezone); | |
| $end_timestamp = strtotime("+$raw_duration min", $start_timestamp); | |
| $end_yyyy_mm_dd = date('Y-m-d', $end_timestamp); // YYYY-MM-DD | |
| // put what we want into hidden GF fields to go into GF entry (and therefore post custom fields, if applicable) | |
| $_POST['input_7'] = $start_timestamp; // hidden text field ID is 7, e.g. 1436283000 | |
| $_POST['input_9'] = $end_timestamp; // e.g. 1436288400 | |
| $_POST['input_11'] = $end_yyyy_mm_dd; // e.g. 2015-07-07 | |
| } | |
| add_action('gform_pre_submission_4', 'cp_gf_tour_datetime_fixes'); // Form #4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment