如何计算夜班和周日的额外工资
我在 MySQL 中做了一个表,以日期时间类型存储工作班次的开始和结束时间。我需要从中计算出两件事(在 MySQL 中)。首先,晚上 10 点到早上 6 点之间某个班次工作了多少分钟,以及周日工作了多少分钟。周日应该优先于那些夜间时间,并且不应该将那些周日早上算两次。
先感谢您。
D.
顺便说一句,我尝试了这个,但没有成功:
SELECT *,
IF(DAYNAME(start)=DAYNAME(end),
IF(TIME(start)<='6:00:00' AND TIME(end)<='6:00:00',TIME(end)-TIME(start),
IF(TIME(start)>='22:00:00' AND TIME(end)>='22:00:00',TIME(end)-TIME(start),
IF(TIME(start)>='6:00:00' AND TIME(end)<='22:00:00',0,
IF(TIME(start)<='6:00:00' AND TIME(end)<='22:00:00','6:00:00'-TIME(start),
IF(TIME(start)>='6:00:00' AND TIME(end)>='22:00:00',TIME(end)-'22:00:00',NULL)
)
)
)
),
IF(TIME(start)<='22:00:00' AND TIME(end)<='6:00:00','2:00:00'+TIME(end),
IF(TIME(start)<='22:00' AND TIME(end)>='6:00:00','8:00:00',
IF(TIME(start)>='22:00:00' AND TIME(end)<='6:00:00','24:00:00'-TIME(start)+TIME(end),
IF(TIME(start)>='22:00:00' AND TIME(end)>='6:00:00','24:00:00'-TIME(start)+'6:00:00',NULL)
)
)
)
) AS ExtraTime FROM `work`
I did a table in MySQL storing start and end times of working shifts in datetime type. I need to calculate two things from them (in MySQL). First of all, how many minutes someone has worked in given shift between 10pm and 6am, as well as how many minutes someone has worked on a sunday. Sunday should have priority over those night times, and it shouldn't count those sunday mornings twice.
Thank you in advance.
D.
I tried this btw, but it didn't work:
SELECT *,
IF(DAYNAME(start)=DAYNAME(end),
IF(TIME(start)<='6:00:00' AND TIME(end)<='6:00:00',TIME(end)-TIME(start),
IF(TIME(start)>='22:00:00' AND TIME(end)>='22:00:00',TIME(end)-TIME(start),
IF(TIME(start)>='6:00:00' AND TIME(end)<='22:00:00',0,
IF(TIME(start)<='6:00:00' AND TIME(end)<='22:00:00','6:00:00'-TIME(start),
IF(TIME(start)>='6:00:00' AND TIME(end)>='22:00:00',TIME(end)-'22:00:00',NULL)
)
)
)
),
IF(TIME(start)<='22:00:00' AND TIME(end)<='6:00:00','2:00:00'+TIME(end),
IF(TIME(start)<='22:00' AND TIME(end)>='6:00:00','8:00:00',
IF(TIME(start)>='22:00:00' AND TIME(end)<='6:00:00','24:00:00'-TIME(start)+TIME(end),
IF(TIME(start)>='22:00:00' AND TIME(end)>='6:00:00','24:00:00'-TIME(start)+'6:00:00',NULL)
)
)
)
) AS ExtraTime FROM `work`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工作时间:
周日工作时间:
minutes worked:
minutes worked on Sunday:
在 PHP 中,尝试以下代码(警告:尚未经过测试):
只需将字符串替换为适合您的计算机的值即可。
广告@m。
In PHP, try this code (warning: it hasn't been tested):
Simply replace the strings with the appropriate values for your computer.
Ad@m.