The Difference between time elements

mamies

Member
Hi All,

I currently have two time elements to indicate the start and the finish of a task.

What I would like to know is how would I calculate the duration that it took for this task.

Thanks,
 
Thanks for the reply troester.

I should have mentioned that I had already looked at the common tasks. I can get the date difference to work if I use the date element but I can't seem to get the time element to produce any results. I am also using the time element instead of date element.

Thanks,
 
I have found a solution.

I was re-reading an answer that Troaster gave me in another thread about arrays and came up with the following script. This seems to work OK.

PHP:
$irrstarthour = $formModel->formData['data_pivot_irrigation___irrigation_start'][0];
$irrstartmin = $formModel->formData['data_pivot_irrigation___irrigation_start'][1];
$irrstophour = $formModel->formData['data_pivot_irrigation___irrigation_stop'][0];
$irrstopmin = $formModel->formData['data_pivot_irrigation___irrigation_stop'][1];
$irrstart = ($irrstarthour * 60) + $irrstartmin;
$irrstop = ($irrstophour * 60) + $irrstopmin;
$irrdec = $irrstop - $irrstart;
$hour = floor($irrdec / 60);
$minb = $irrdec - ($hour * 60);
$min = sprintf("%02d",(int)$minb);
$timediff = $hour . ':' . $min;
$formModel->updateFormData("data_pivot_irrigation___irrigation_duration", $timediff, true);

To have it record seconds as well you could use below (I haven't tested so there maybe an issue with the seconds but it would be fairly close.)

PHP:
$irrstarthour = $formModel->formData['data_pivot_irrigation___irrigation_start'][0];
$irrstartmin = $formModel->formData['data_pivot_irrigation___irrigation_start'][1];
$irrstartsec = $formModel->formData['data_pivot_irrigation___irrigation_start'][2];
$irrstophour = $formModel->formData['data_pivot_irrigation___irrigation_stop'][0];
$irrstopmin = $formModel->formData['data_pivot_irrigation___irrigation_stop'][1];
$irrstopsec = $formModel->formData['data_pivot_irrigation___irrigation_stop'][2];
$irrstart = ($irrstarthour * 3600) + ($irrstartmin * 60) + $irrstartsec;
$irrstop = ($irrstophour * 3600) + ($irrstopmin * 60) + $irrstartsec;
$irrdec = $irrstop - $irrstart;
$hour = floor($irrdec / 3600);
$minb = $irrdec - ($hour * 3600);
$minc = floor($minb / 60);
$min = sprintf("%02d",(int)$minc);
$secb = $minb - ($minc * 60);
$sec = sprintf("%02d",(int)$secb);
$timediff = $hour . ':' . $min . ':' . $sec;
$formModel->updateFormData("data_pivot_irrigation___irrigation_duration", $timediff, true);

Obviously your table names would need to be changed. I hope someone else can find this useful.
 
Hello Matthew

Thank you very much. This will definately help me with my other tasks. For now I just need to create a calc that will convert the number of minutes in a field to hours and minutes.

In the list is a field called "duration_total" and it is a decimal number of minutes, e.g. 1245. I just need to create a calc element that will change the decimal value to hh:mm.

Jean
 
Date / time calculations and formatting are usually best done with PHP's DateTime object, and the various functions PHP provides for calculating and formatting them. So the usual way would be something like ...

Code:
$minutes = (int) $data['yourtable___duration_total'];
$seconds = $minutes * 60;
$from = new DateTime("@0");
$to = new DateTime("@$seconds");
return $from->diff($to)->format('%h:%i');
... which uses the DateTime diff() method to calculate the difference between 0 and your duration (in seconds), and the format() method to format it as hours:minutes. Mr Google can tell you all about PHP's DateTime object and it's methods.
NOTE - in calc's it's safer to use $data than $formModel->formData, as the latter is only available during form submission, and calc elements can be calculated during list / details / form display as well, in which case data is somewhere else in the model(s). So in the calc element we make sure we put a copy of your row data in $data for you to access, so you don't have to worry about where to find it.
-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top