Advertisement
Guest User

Gravity Forms - calculate time between two fields

a guest
Aug 6th, 2012
1,406
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_action("gform_pre_submission","pre_submission_handler");
  2.  
  3. function pre_submission_handler($form) {
  4.   // get input from form
  5.   $date = $_POST["input_1"];
  6.   $start = $_POST["input_11"];
  7.   $end = $_POST["input_5"];
  8.  
  9.   // convert date and time arrays into datetime formats
  10.   $startdate = date_create_from_format('m/d/Y@h:i a', $date . "@". $start[0].":".$start[1]." ".$start[2]);
  11.   $enddate =   date_create_from_format('m/d/Y@h:i a', $date . "@". $end[0].":".$end[1]." ".$end[2]);
  12.  
  13.   //convert datetimes into seconds to compare
  14.   $starttime = strtotime($startdate->format('Y-m-d H:i:s'));
  15.   $endtime = strtotime($enddate->format('Y-m-d H:i:s'));
  16.  
  17.   // check to see if the times span overnight
  18.   if($starttime > $endtime)
  19.     $endtime = strtotime($enddate->format('Y-m-d H:i:s') . " +1 day");
  20.  
  21.   // perform calculation
  22.   $diff = floor(($endtime - $starttime)/60);
  23.  
  24.   $_POST["input_50"] = $diff;
  25. }
Advertisement
RAW Paste Data Copied
Advertisement