[英]Sum Time SQlite PHP over 24Hrs
I have a list of times that I need to sum. 我有一个需要总结的时间清单。 I have two tables: workers and schedule.
我有两个表:worker和schedule。
CREATE TABLE schedule (
id,
worker_id,
date,
start_time,
end_time,
hours
SELECT w.name, w.salary, time(sum(strftime('%s', hours) - strftime('%s', '00:00:00')), 'unixepoch') AS total_hours
FROM workers w
JOIN schedule s ON s.worker_id = w.id
WHERE date between '2018-09-17' AND '2018-09-21'
GROUP BY w.name
The column hours contain the total of hours worked for every day. 列小时数包含每天的总工作时间。 I found some other posts, but not the solution that I need.
我找到了其他一些帖子,但没有找到所需的解决方案。
Image schedule table 影像时间表
This request works fine if I sum values under 24Hrs : example 10:00 + 07:00 the total will be : 17:00, but if I sum more times : 10:00 + 07:00 + 09:00 I will get :02:00.
如果我求和以下24Hrs的值,则此请求工作正常:例如10:00 + 07:00总和为:17:00,但是如果我求和更多时间:10:00 + 07:00 + 09:00,我将得到: 02:00。
I don't know what I am doing wrong. 我不知道我在做什么错。
The result what I am looking for : 结果我正在寻找:
Worker | Salary | Total Hours
John Doe | $28.00 | 26:00
worker 1 | $30.00 | 20:15
worker 2 | $25.00 | 42:30
Just represent the value as decimal hours, so 10:30 would be represented instead at 10.5: 仅将值表示为十进制小时数,因此将以10:30表示为10.5:
SELECT w.name, w.salary,
sum(strftime('%s', hours) - strftime('%s', '00:00:00')) / (60.0 * 60) as decimal_hours
FROM workers w JOIN
schedule s
ON s.worker_id = w.id
WHERE date between '2018-09-17' AND '2018-09-21'
GROUP BY w.name;
You can format this back into a string of the form HH:MM, if you really, really want. 如果确实需要,可以将其格式化为HH:MM格式的字符串。 I suggest that you keep it as decimal hours or minutes, though.
我建议您将其保留为十进制小时或分钟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.