【问题标题】:php get total worked hours from start time to end time with multiple entries for dayphp获取从开始时间到结束时间的总工作时间,当天有多个条目
【发布时间】:2016-08-26 22:54:37
【问题描述】:

我正在尝试构建一个时钟输出系统。当尝试获取用户在给定日期的工作时间报告时,我需要计算工作小时数。我有一个 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 循环中,但我总是得到错误的值

【问题讨论】:

  • 我想你可以通过解析数组来做到这一点,但在这种情况下,我想我会很想在查询本身中做到这一点。
  • strtotime($val['startTime']); 代码的输出是什么?这是一个 unix 时间戳,而不是格式化的日期......
  • 是的,它是一个时间戳

标签: php mysql arrays associative-array


【解决方案1】:

问题在于你的最后一行: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";

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 1970-01-01
      • 2023-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多