找单独工作的分钟

时间:2017-07-16 16:44:40

标签: php time schedule

我有工作人员的时间列表。我需要知道是否有任何员工单独工作,以及他们当天独自工作多少分钟

| 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 |

在Andrea的帮助下,以下是第一个也是最后一个单独工作的人的代码,但它并不安静。因为如果有3个人在不同的时间独自工作,那就会产生问题。

$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;

使用以下数组,您会看到第一个用户的分钟数需要超过5分钟,因为他晚上独自工作更多。

$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");

2 个答案:

答案 0 :(得分:1)

一种方法是创建事件列表

Staff   Event Time
1       S     11:05
1       E     20:00
2       S     11:00
2       E     17:00 etc.

然后按时间顺序排序并迭代事件。

如果一个人开始轮班,那么将一个添加到staffOnShift变量,如果是结束,则减去一个。

如果staffOnShift == 1&&该事件是一个班次开始,然后将自上次事件以来的分钟数添加到minutesAlone变量。

希望这有助于您开始使用。

补充说明

是的,开始和结束需要被视为单独的事件,所以在排序后你会有一个像

这样的列表
Staff   S/E   Time       staffOnShift       minutesAlone
2        S    11:00           1               0
1        S    11:05           2               5
4        S    13:00           3               5
2        E    17:00           2               5
3        S    19:00           3               5
5        S    19:00           4               5
1        E    20:00           3               5
4        E    20:00           2               5
3        E    03:00           1               5
5        E    03:00           0               5 + 0 = 5

通过此列表进行迭代并在有人开始轮班时添加并在某人轮班结束时减去,这将为您提供每个赛事轮班的人数,我已将其列入第4栏。

如果staffOnShift == 1(在加或减之前),请将自上次事件以来的分钟数添加到minutesAlone变量中。 (这不是我上面所说的,但逻辑表有助于澄清)

使用您的数据,唯一符合这些条件的事件是第二个和最后一个。所以会增加5分钟,因为一开始就有一个人在轮班。最后也满足标准,但由于最后两个事件之间没有分钟,因此不会增加总数。

答案 1 :(得分:0)

知道了!

花了一些时间,但我找到了解决方案 它在阵列中确实有一个额外的项目,但由于持续时间为零,因此不应该导致问题。

$staff = array(1,2,3,4,5);
$start = array("11:05", "11:00", "19:00", "13:00", "19:00");
$end = array("16:00", "17:00", "03:00", "16:00", "03:05");

// Add staff number to end of time ex 11:00 => 11:00#2
For($i=0; $i<count($start);$i++){
    $start[$i] .= "#" . $staff[$i];
    $end[$i] .= "#" . $staff[$i];

}
$t = array_merge($start,$end); // create one long array with all in and out times
sort($t);

// Multisport is needed to get all arrays in time order as reference
array_multisort($start, $end, $staff);

// Find first start time (11:00) and slice array thwre, build string
$test = implode(PHP_EOL,array_slice($t, array_search($start[0], $t)));

// Find the times before first start (night end times) and add them last in string
$test .= PHP_EOL . implode(PHP_EOL,array_slice($t, 0,array_search($start[0], $t)));
$times = explode(PHP_EOL, $test); // explode to make it array again
 // Var_dump($times);

$WhoIsInDaHouse = array("dummy"); // add a dummy variable since 0=false in later if
$j=0;
for($i=0; $i<count($times);$i++){
    $TimePerson = explode("#", $times[$i]);
    $Time = $TimePerson[0];
    $person = $TimePerson[1];


    $inout = array_search($person, $WhoIsInDaHouse); //is person in house and about to leave?
    If($inout != false){ //if person enter work false, if true: key of person leaving in $WhoIsInDaHouse
        //Here $person is leaving work
        Unset($WhoIsInDaHouse[$inout]);

        If(count($WhoIsInDaHouse) == 2){ // someone will now be alone since we have a dummy
            $Alone[$j]["start"] = $Time;
            $Alone[$j]["who"] = array_slice($WhoIsInDaHouse, -1)[0];
        }elseif(count($WhoIsInDaHouse) == 1 && $prevcount == 2){
            // Only dummy left
            $Alone[$j]["end"] = $Time;
            $Alone[$j]["duration"] = strtotime($Alone[$j]["end"])-strtotime($Alone[$j]["start"]);
            $j++;
        }
    }Else{
        // Here person enters work
        $WhoIsInDaHouse[] = $person;

        If(count($WhoIsInDaHouse) == 2){ // someone is entering alone
            $Alone[$j]["start"] = $Time;
            $Alone[$j]["who"] = $person;
        }elseif(count($WhoIsInDaHouse)>2 && $prevcount == 2){ // not alone anymore
            $Alone[$j]["end"] = $Time;
            $Alone[$j]["duration"] = strtotime($Alone[$j]["end"])-strtotime($Alone[$j]["start"]);
            $j++;
        }
    }
    $prevcount = count($WhoIsInDaHouse);
}
Var_dump($Alone);

看到美女运动https://3v4l.org/dCL2H

我花了很长时间才弄明白我需要一个假人。谁知道假人可能有用?