SQL(?):计算两个日期时间值之间的时间
当我只需要计算 08:00:00-16:00:00 之间的时间时,计算从 MySQL 获取的两个日期时间值之间的时间的最佳方法是什么。
例如,如果我的值是 2008-10-13 18:00:00 和 2008-10-14 10:00:00,那么时间差应该是 02:00:00。
我可以使用 SQL 来完成此操作吗?或者最好的方法是什么? 我正在构建一个网站并使用 PHP。
谢谢您的回答。
编辑:确切的事情是,我正在尝试计算“票”在工作时间内处于特定状态的时间。 时间可能需要几周。
EDIT2:我计算实际时差没有问题,但减去每天的休息时间 00:00:00-08:00:00 和 16:00:00-00:00:00 。
-萨穆里
What is the best way to count the time between two datetime values fetched from MySQL when I need to count only the time between hours 08:00:00-16:00:00.
For example if I have values 2008-10-13 18:00:00 and 2008-10-14 10:00:00 the time difference should be 02:00:00.
Can I do it with SQL or what is the best way to do it? I'm building a website and using PHP.
Thank you for your answers.
EDIT: The exact thing is that I'm trying to count the time a "ticket" has been in a specific state during working hours. The time could be like a couple weeks.
EDIT2: I have no problems counting the actual time difference, but substracting that off-time, 00:00:00-08:00:00 and 16:00:00-00:00:00 per day.
-Samuli
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TIMEDIFF 函数
The TIMEDIFF function
我认为你应该在自己的代码中计算差异,而不是使用更复杂的 SQL 语句,因为:
I think you should calculate the difference in your own code instead of using a more-complex SQL sentence because:
中会更快
您可能想在 PHP 中尝试一下,但我认为在 DB 代码 返回数组中的差异
You may want to try it in PHP, but I'm thinking it'll be faster in DB
code to return difference in an array