php 获取一天的多个条目从开始时间到结束时间的总工作时间

php get total worked hours from start time to end time with multiple entries for day

我正在尝试构建一个时钟输入输出系统。当试图获取用户在给定日期的工作小时数报告时,我需要计算总工作小时数。我有一个 startTime(时间戳)和 endTime(时间戳),但我可以在任何给定的一天为用户提供多个条目,他们打卡去打卡打卡,然后在吃午饭后打卡,等等。

 array
  (
        [theUser] => Bob Johnson
        [timeId] => 9
        [user_id] => 1
        [weekNo] => 34
        [clockYear] => 2016
        [entryDate] => 2016-08-21
        [startTime] => 2016-08-21 21:57:01
        [endTime] => 2016-08-21 22:45:15
        [updated_at] => 2016-08-21 22:45:15
        [totTime] => 00:48:14
    ) 

    [9] => Array
    (
        [theUser] => Bob Johnson
        [timeId] => 16
        [user_id] => 1
        [weekNo] => 34
        [clockYear] => 2016
        [entryDate] => 2016-08-24
        [startTime] => 2016-08-24 01:00:00
        [endTime] => 2016-08-24 05:15:00
        [updated_at] => 
        [totTime] => 04:15:00
    )
    (
        [theUser] => Bob Johson
        [timeId] => 15
        [user_id] => 1
        [weekNo] => 34
        [clockYear] => 2016
        [entryDate] => 2016-08-24
        [startTime] => 2016-08-24 01:00:00
        [endTime] => 2016-08-24 05:15:00
        [updated_at] => 
        [totTime] => 04:15:00
    )

    (
        [theUser] => Bob Johnson
        [timeId] => 18
        [user_id] => 1
        [weekNo] => 34
        [clockYear] => 2016
        [entryDate] => 2016-08-24
        [startTime] => 2016-08-24 09:59:00
        [endTime] => 2016-08-24 10:45:00
        [updated_at] => 2016-08-24 10:19:31
        [totTime] => 00:46:00
    )

我需要用总工作时间制作新数组我想实现这样的目标

         (
                [theUser] = Bob Johnson
                [weekNo] = 34
                [Year] = 2016
                [entryDate] = 2016-08-24
                [totalHoursWorked] = 09:16:00
                [overTime] = 01:16:00
            )

如果尝试服用

,谁能帮忙解决这个问题
          $startTime = strtotime($val['startTime']);
          $endTime = strtotime($val['endTime']);
          $tot += $endTime - $startTime;

          echo date('H:i:s', $tot)

在 foreach 循环中,但我总是得到错误的值

问题出在你的最后一行:echo date('H:i:s', $tot)

您应该改用 gmdate

echo gmdate('H:i:s', $tot);

或者用简单的数学代码格式化:

$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$hours:$minutes:$seconds";

使用Carbon这样的库会更方便你的计算和格式化