简体   繁体   English

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

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

Im trying to build a clock in clock out system. 我试图建立一个时钟输出系统。 When trying to get a report of hours worked by a user for a given day i need to total the number of hours worked. 尝试获取给定日期的用户工作时间报告时,我需要总计工作时间。 I have a startTime (timestamp) and endTime (timestamp) but i can have multiple entries for a user on any given day whre they clock in go to break clock out then clcok in after and out for lunch and so on. 我有一个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
    )

I need to make new array with the total hours worked summed up I would like to achieve something like this 我需要用总工作时间来制作新的阵列,我想实现这样的目标

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

Can anyone help with this please if tried taking 如果尝试服用,任何人都可以帮忙吗

          $startTime = strtotime($val['startTime']);
          $endTime = strtotime($val['endTime']);
          $tot += $endTime - $startTime;

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

in a foreach loop but i always get wrong values 在foreach循环中,但我总是得到错误的值

The problem is with your last line: echo date('H:i:s', $tot) 问题出在最后一行: echo date('H:i:s', $tot)

You should use gmdate instead: 您应该改用gmdate

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

Or format it with a simple math code: 或使用简单的数学代码对其进行格式化:

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

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

使用Carbon等库将更方便您进行计算和格式化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取每个日期 PHP 的 start_time 和 end_time 的总小时数? - Get total hours from start_time and end_time for each date PHP? 使用 PHP 从具有 start_time 和 end_time 的多维数组中查找总小时数? - Find total hours from multi-dimensional array with start_time and end_time using PHP? 如何使用多维数组以及重叠检查从开始时间和结束时间获取总小时数? - How to get total hours from start time and end time with multi dimensional array along with over lapping check? 如何使用php中的12小时格式获取从开始时间到结束时间的总小时数 - How to get the total hr from start time to end time using 12 Hour Format in php 如何在PHP中获取从开始时间到结束时间的总小时数 - How to get the total hour from starting time to end time in php PHP时间范围,获取总时间,晚上,晚上,周末和节假日 - PHP Time frame, get total hours, evening hours, night hours, weekend hours and holidays 如何参考签入和第二天结帐时间在MYSQL中获得带日期和时间的总工作时间 - how to get total no of working hours with date & time in MYSQL with reference to checkin & next day checkout time 查询mysql以计算一天的总工作时间的可能方法 - Possible way to query mysql for calculate total time worked in a day 当开始时间和结束时间为12小时格式时,使用php以小时和分钟为单位计算时间差异 - calculate time differnece in hours and minutes using php when start time and end time is in 12 hour format 如何使用php和javascript实现从开始到结束日期的总经过时间 - How to implement total elapsed time from start to end date using php and javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM