[英]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.