I have a form that has a date field and then a Time Range field. The Time Range field is a radio button field. The user picks a time range as opposed to picking a specific time. The value of the radio buttons is the start times for the range. (i.e. Radio button text says “9:00AM - 11:00AM”, the value is “09:00:00”.) I then have a hidden field where I want to dynamically insert the date selected and the value of the radio button selected so that when it sends the values to Salesforce it is combined into 1 field and is formatted to a datetime. In my functions.php file, I inserted the following code:
// Gravity Form functions
add_action( ‘gform_pre_submission_56’, ‘pre_submission_handler’ );
function pre_submission_handler( $form ) {
// Fill hidden field 29 with the values of input fields 23 and 25
// Format date as YYYYMMDD
$appt_date = date(“Ymd”, rgpost( ‘input_23’ ));
// Convert the appointment time string to time
$appt_time = strtotime(rgpost( ‘input_25’ ));
// Combine the date and time fields
$appt_date_time = date(“Ymd H:i:s”, strtotime("$appt_date $appt_time"));
// Fill the combined date and time into the Appointment_Date_and_Time__c field
$_POST[‘input_29’] = $appt_date_time;
}
Unfortunately, this doesn’t seem to be working. When I submit the form, the field in Salesforce is populated with 1/1/1970 12:00 AM, even though I have selected a date from the calendar and did select a radio button. I did some tests and know that the issue is with my php code. I am a newbie to PHP. Can someone please tell me what I am doing wrong? Thank you
Hi Lara. I added some logging statements to your code so that you can see what is not working. Please first enable Gravity Forms logging:
Then, use this version of your code with additional logging statements:
// Gravity Form functions
add_action( 'gform_pre_submission_56', 'pre_submission_handler' );
function pre_submission_handler( $form ) {
GFCommon::log_debug( __METHOD__ . '(): The POST => ' . print_r( $_POST, true ) );
// Fill hidden field 29 with the values of input fields 23 and 25
// Format date as YYYYMMDD
$appt_date = date( "Ymd", rgpost( 'input_23' ) );
GFCommon::log_debug( __METHOD__ . "(): appt_date is {$appt_date}." );
// Convert the appointment time string to time
$appt_time = strtotime( rgpost( 'input_25' ) );
GFCommon::log_debug( __METHOD__ . "(): appt_time is {$appt_time}." );
// Combine the date and time fields
$appt_date_time = date("Ymd H:i:s", strtotime("$appt_date $appt_time"));
GFCommon::log_debug( __METHOD__ . "(): appt_date_time is {$appt_date_time}." );
// Fill the combined date and time into the Appointment_Date_and_Time__c field
$_POST['input_29'] = $appt_date_time;
}
Test the form, then view the Gravity Forms Core log on the Forms → Settings → Logging page. Send a link to that log file if you need help interpreting what was logged.
Thanks for your help. I did what you said and it logged it the first time. However, every time I submit the form after that new log messages are not added to the log file. I even tried deleting the log file. Then I submitted the form again and then viewed the log file. It still showed the information (log date and time) from the first log file. Any suggestions?
Looking at your code again, it looks like this is going to be the problematic line (or at least one problem if there are more than one.) You are calling strtotime on a string which is “$appt_date $appt_time”. Those two won’t be converted to values of date and time from your submission, with a space in between, to strtotime won’t return a valid value. If you can get the logging, we can confirm that this is the issue or if there are more.