Better Time Calculations

Labels

This Discussion is public

Notifications

Hello everyone, so over the past few weeks i have really been getting my hands dirty and had the need to calculate time spent on a certain form. This form is one that when an employee is completed with a job, they put what time they started and ended and signs and then submits.

 

Currently with the hook to calculate time on formidable forms its very limited because if the employee works for example from 10pm to 4am it would calculate it as a negative number and a few other examples that you can try out if you like. so i came up with some code to add to this.

 

Just as a reference, for this to work you cannot use the regular time field. Instead i had to make 3 drop down fields for Hour, Minute and AM/PM. i made my minutes only 15 minute increments but you can do whatever you like. You will need two sets of these drop downs, one for starting time and one for ending time.

The code is here:

add_filter('frm_validate_field_entry', 'calculate_time', 11, 3);
function calculate_time($errors, $field, $value){
if($field->id == 596){  //change this to the field where you want the total to pop into such as a locked text field.
$hourin = $_POST['item_meta'][597]; //this is the drop down for the starting time hour
$hourout = $_POST['item_meta'][600]; //this is the drop down for the ending time hour
$minin = $_POST['item_meta'][598]; //this is the drop down for the starting time minute
$minout = $_POST['item_meta'][601]; //this is the drop down for the ending time minute
$ampmin = $_POST['item_meta'][599]; //this is the drop down for the starting time AM/PM
$ampmout = $_POST['item_meta'][602]; //this is the drop down for the ending time AM/PM

if (($ampmin==AM) and ($ampmout==PM)) { //converting time difference if start - end is AM - PM
$t1 = ($hourin * 60) + $minin;
if ($hourout==12){
$t2 = ($hourout * 60) + $minout;
} else {
$t2 = (($hourout + 12) * 60) + $minout;}
$t3 = $t2 - $t1;
$t4 = floor($t3 / 60);
$t5 = $t3 - ($t4 * 60);
}

else if (($ampmin==PM) and ($ampmout==AM)) { //converting time difference if start - end is PM - AM
if ($hourout==12) {$hourout = 0;}
if ($hourin==12) {$hourin = 0;}
$t1 = ($hourin * 60) + $minin;
$t2 = (($hourout + 12) * 60) + $minout;
$t3 = $t2 - $t1;
$t4 = floor($t3 / 60);
$t5 = $t3 - ($t4*60);
}

else if (($ampmin==AM) and ($ampmout==AM)) { //converting time difference if start - end is AM - AM
$t1 = ($hourin * 60) + $minin;
if ($hourout==12) {$hourout = 0;}
$t2 = ($hourout * 60) + $minout;
if ($t2 > $t1) {
$t3 = $t2 - $t1;
$t4 = floor($t3 / 60);
$t5 = $t3 - ($t4 * 60);
} else {
$t2 = (($hourout + 24) * 60) + $minout;
$t3 = $t2 - $t1;
$t4 = floor($t3 / 60);
$t5 = $t3 - ($t4 * 60);
}}

else if (($ampmin==PM) and ($ampmout==PM)) { //converting time difference if start - end is PM - PM
if ($hourin!=12) {$hourin = ($hourin + 12);}
if ($hourout!=12) {$hourout = ($hourout + 12);}
$t1 = ($hourin * 60) + $minin;
$t2 = ($hourout * 60) + $minout;
if ($t2>$t1) {
$t3 = $t2 - $t1;
$t4 = floor($t3 / 60);
$t5 = $t3 - ($t4 * 60);}
else {
$t2 = (($hourout + 24) * 60) + $minout;
$t3 = $t2 - $t1;
$t4 = floor($t3 / 60);
$t5 = $t3 - ($t4 * 60);
}
}

if ($t5==15) {$t5 = .25;}
if ($t5==30) {$t5 = .50;}
if ($t5==45) {$t5 = .75;}

$finaltime = $t4 + $t5;

$value = $_POST['item_meta'][596] = " " . $finaltime . " Hours";
}
return $errors;
}

So as you can see from the code above the way i calculated this was actually having 4 different calculations depending on whether its is going from AM to PM or vice versa etc.

for my use, i have the end code to convert the minutes to decimal places so i can have for example 8.5 hours or 7.25 hours.

 

This isn't for everyone but i thought i would throw this up here and give back to the great community that has helped me alot in other tasks of mine.

 

Thanks!!

Thanks for taking the time to share, Joseph! We appreciate it!

Reply to this Discussion