designerken

pre_submission_handler

Nov 15th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2.  
  3. add_action("gform_pre_submission","pre_submission_handler");
  4.  
  5. function pre_submission_handler($form) {
  6.   // get input from form
  7.   $date     =   $_POST["input_1"]; //GF 'date picker' using mm/dd/yyyy
  8.   $start    =   $_POST["input_2"]; //GF 'timefield' using military time
  9.   $end      =   $_POST["input_3"]; //GF 'timefield' using military time
  10.  
  11.   // convert date and time arrays into datetime formats
  12.   $startdate = new DateTime( $date . ' ' . $start[0] . ':' . $start[1] ) ;
  13.  
  14.   $enddate = new DateTime( $date . ' ' . $end[0] . ':' . $end[1] ) ;
  15.  
  16.   //convert datetimes into seconds to compare
  17.   $starttime = strtotime( $startdate->format( 'm/d/Y H:i' ) );
  18.   $endtime = strtotime( $enddate->format( 'm/d/Y H:i' ) );
  19.  
  20.   // check to see if the times span overnight
  21.   if($starttime > $endtime)
  22.     $endtime = strtotime($enddate->format('m/d/Y H:i:s') . " +1 day");
  23.  
  24.   // perform calculation
  25.   $diff = floor(($endtime - $starttime)/60/60);
  26.  
  27.   $_POST["input_7"] = $diff;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment