I have list of times for staff. I need to find out if any of the staff was working alone and how many minutes they were working alone for the day
| staff| start | end |
|:--- |:--- |:--- |
| 1 | 11:05 | 20:00 |
| 2 | 11:00 | 17:00 |
| 3 | 19:00 | 03:00 |
| 4 | 13:00 | 20:00 |
| 5 | 19:00 | 03:00 |
With Andrea help, following is the code that gets the first and last person who was working alone with alone minutes, but its not quiet right. Because if there was 3 people with different times that worked alone, it will give problem.
$staff = array(1,2,3,4,5);
$start = array("11:05", "11:00", "19:00", "13:00", "19:00");
$end = array("20:00", "17:00", "03:00", "20:00", "03:05");
array_multisort($start, $end, $staff);
$aloneStart = (strtotime($start[1]) - strtotime($start[0]))/60; // first and second items are the ones that may be working alone at start
$aloneEnd = (strtotime($end[count($end)-1]) - strtotime($end[count($end)-2]))/60; // last and second to last are the ones that may be working alone at end
if($aloneStart > 0){
$staffAloneStart = $staff[0]; //must be the first who worked alone
echo "minutes alone at start: " . $aloneStart . " and it was " . $staffAloneStart . "\n";
}
if($aloneEnd > 0){
$staffAloneEnd = $staff[count($end)-1]; // must be the last to end that worked alone
echo "minutes alone at end: " . $aloneEnd . " and it was " . $staffAloneEnd . "\n";
}
$aloneTime = intval($aloneStart)+intval($aloneEnd);
echo "total time alone " . $aloneTime;
with following array, you will see the minutes for first user needs to be more then 5 minutes, because he is working alone more at evening.
$staff = array(1, 2, 3, 4, 5);
$start = array("11:05", "11:10", "19:00", "13:00", "19:00");
$end = array("20:00", "17:00", "03:00", "16:00", "03:00");
Got it!
It took some time but I found a solution.
It does have one extra item in the array but since the duration is zero it shouldn't cause an issue.
And see the beauty run https://3v4l.org/dCL2H
It took me a long time to figure out I needed a dummy. Who knew a dummy could be useful?
One approach would be to create a list of events
Then sort them in time order and iterate through the events.
If a person starts a shift then add one to a staffOnShift variable, if it is an end then subtract one.
If the staffOnShift == 1 && the event is a shift start then add the minutes since the last event to the minutesAlone variable.
Hope this helps you to get started.
Additional explanation
Yes, the start and end need to be treated as separate events so after sorting you would have a list like
Iterate through this list and add when someone starts a shift and subtract when someone’s shift ends, this will give you the number of people on shift at each event, I have put it in column 4.
If staffOnShift == 1 (before the add or subtract), add the minutes since the last event to the minutesAlone variable. (This isnt quite what I said above but the logic table helped to clarify)
With your data the only events to meet those criteria are the second and last. So 5 minutes would be added because there is 1 person on shift at the start. The criteria are met at the end as well but as there are no minutes between the last two events it would not add to the total.