I need to calculate the difference, "for example in minutes", of a series of intervals, however there is no limit, because there may be N time records, between "tipo_2_pause
" and "tipo_3_return
".
Excluding "tipo_1_start
" and "tipo_4_end
", both "tipo_2_pause
" and "tipo_3_return
" is an array with N records.
As an example, I need to calculate the difference between the "tipo_4_end" and the "tipo_1_start".... ok... this is easy... but I'm not able to find a way in which I can calculate always considering the respective pairs between "tipo_2_pause" and "tipo_3_return".
In the example, the first pair between "tipo_2_pause" and "tipo_3_return", I hope to get the difference... which in this case is equal to 10min....in the second pair, by chance, it could also be 10min... but it could be any time .. but if there is no pair, the result would be 0.
Objective:
Being able to calculate the difference in minutes between each item in the array "tipo_2_pause
" that pairs with the array "tipo_3_return
".
You're right that the key to getting pairs is to use the length of the shorter array.
Then you just apply a for-loop.
Somewhat verbose (for clarity) first pass:
$numPairs = min(count($tipo2), count($tipo3));
$duration = 0;
for($i=0; $i<$numPairs; $i++){
$start = $tipo2[$i];
$end = $tipo3[$i];
$duration += minutes_between($start, $end); // implementing minutes_between is left as an exercise.
}