cliffordp
9/1/2015 - 12:35 PM

Combine several Gravity Forms form fields into a hidden "timestamp" form field using PHP's strtotime()

Combine several Gravity Forms form fields into a hidden "timestamp" form field using PHP's strtotime()

<?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