dongpu1315 2017-01-24 05:36
浏览 23
已采纳

我想根据班次开始时间及其所有开始和结束时间在php中计算员工的所有非工作时间

Here is data

Shift Start Time======== 07:00:00
Shift End Time========== 15:00:00
Array
(
    [0] => stdClass Object
        (               
            [startTime] => 07:00:00
            [endTime] => 07:50:00
            [elapsed_mins] => 50                
        )
[1] => stdClass Object
    (                       
        [startTime] => 08:50:00
        [endTime] => 09:50:00
        [elapsed_mins] => 20

    )
[2] => stdClass Object
    (            
        [startTime] => 09:50:00
        [endTime] => 10:10:00
        [elapsed_mins] => 20           
    ))

here as you can see there the first worked time same as shift start time so that is fine but in second record there is a gap from 7:50:00 to 08:50:00 so I want this non working record also into the same array as 2nd object and in last you can see there is no record after 10:10:00 to 15:00:00(shift end time) so i want this record also into the same array. finally there should be five elements into the array. Please help.

according to the above question, desired result should be.

Array
(
    [0] => stdClass Object
        (               
            [startTime] => 07:00:00
            [endTime] => 07:50:00
            [elapsed_mins] => 50                
        )
[1] => stdClass Object
        (               
            [startTime] => 07:50:00
            [endTime] => 08:50:00
            [elapsed_mins] => 60                
        )
[2] => stdClass Object
    (                       
        [startTime] => 08:50:00
        [endTime] => 09:50:00
        [elapsed_mins] => 60

    )
[3] => stdClass Object
    (            
        [startTime] => 09:50:00
        [endTime] => 10:10:00
        [elapsed_mins] => 20           
    )
[4] => stdClass Object
    (            
        [startTime] => 10:10:00
        [endTime] => 15:00:00
        [elapsed_mins] => 290           
    )
)
  • 写回答

2条回答 默认 最新

  • doushan2224 2017-01-24 06:31
    关注

    Have created a custom code as per your desired output

    $shift_start_time = '07:00:00';
    $shift_end_time   = '15:00:00';
    
    $i             = 0;
    $last_end_time = $shift_start_time;
    foreach ($array as $key => $value)
    {
        if ($last_end_time != $value->startTime)
        {
            $sTime   = $last_end_time;
            $eTime   = $value->startTime;
            $elapsed = (strtotime($eTime) - strtotime($sTime)) / 60;
            array_splice($array, $i, 0, (object) array(array('startTime' => $sTime, 'endTime' => $eTime, 'elapsed' => $elapsed)));
        }
        $last_end_time = $value->endTime;
        $i++;
    }
    
    $last_worked = end($array);
    
    if ($last_worked->endTime !== $shift_end_time)
    {
        $startTime = $last_worked->endTime;
        $endTime   = $shift_end_time;
        $elapsed   = (strtotime($endTime) - strtotime($startTime)) / 60;
    
        $array[] = (object) array('startTime' => $startTime, 'endTime' => $endTime, 'elapsed' => $elapsed);
    }
    
    
    var_dump($array);
    

    This will return below output

    array (size=5)
      0 => 
        object(stdClass)[1]
          public 'startTime' => string '07:00:00' (length=8)
          public 'endTime' => string '07:50:00' (length=8)
          public 'elapsed_mins' => string '50' (length=2)
      1 => 
        object(stdClass)[2]
          public 'startTime' => string '07:50:00' (length=8)
          public 'endTime' => string '08:50:00' (length=8)
          public 'elapsed_mins' => string '60' (length=2)
      2 => 
        object(stdClass)[3]
          public 'startTime' => string '08:50:00' (length=8)
          public 'endTime' => string '09:50:00' (length=8)
          public 'elapsed_mins' => string '60' (length=2)
      3 => 
        object(stdClass)[4]
          public 'startTime' => string '09:50:00' (length=8)
          public 'endTime' => string '10:10:00' (length=8)
          public 'elapsed_mins' => string '20' (length=2)
      4 => 
        object(stdClass)[5]
          public 'startTime' => string '10:10:00' (length=8)
          public 'endTime' => string '15:00:00' (length=8)
          public 'elapsed' => int 290
    

    Please note: Before posting question on SO please try doing it yourself and post your code here. People on SO are here to help you solve problems in your code, not to help you create the whole code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?