简体   繁体   English

计算两个时间戳之间的小时差?

[英]Calculate hour difference between two timestamps?

I'm working on a PHP project with Firebird, about working hours.我正在使用 Firebird 进行一个 PHP 项目,关于工作时间。 Basically, every worker has a card, which he stamps on entering and leaving the building.基本上,每个工人都有一张卡,他在进出大楼时盖上章。 Each stamp is recorded into the ARCHIVE table, with date ( DATUM ), id ( NREC ) and user_id ( NCODE ).每个戳记都记录到ARCHIVE表中,带有日期 ( DATUM )、id ( NREC ) 和 user_id ( NCODE )。 What I have to do is get the last and first time of each day, and calculate hours.我要做的是获取每天的最后一次和第一次,并计算小时数。 So to get the first and last, I used this:所以为了获得第一个和最后一个,我使用了这个:

SELECT MAX(NREC)
FROM ARCHIVE
WHERE DATUM BETWEEN '$day.$month.$year' AND '$day+1.$month.$year';

SELECT MIN(NREC)
FROM ARCHIVE
WHERE DATUM BETWEEN '$day.$month.$year' AND '$day+1.$month.$year';

I save both max and max in a variable, then I select the both dates and calculate.我将 max 和 max 都保存在一个变量中,然后选择这两个日期并进行计算。 But that are 4 queries.但那是 4 个查询。 Is there any simpler/easier way?有没有更简单/更简单的方法? Also, DATUM is a timestamp此外, DATUM是一个时间戳

Could try:可以试试:

SELECT DATUM FROM ARCHIVE JOIN (
    SELECT MIN(NREC) AS minid, MAX(NREC) as maxid FROM ARCHIVE 
    WHERE DATUM  DATUM BETWEEN '$day.$month.$year' AND '$day+1.$month.$year' 
    AND NCODE = user_id) 
as mm ON mm.maxid = ARCHIVE.NREC OR mm.minid=ARCHIVE.NREC 

If Firebird works anything like MySQL this should give you two timestamps.如果 Firebird 像 MySQL 那样工作,这应该给你两个时间戳。

What about关于什么

SELECT CAST(DATUM AS DATE), MAX(NREC), MIN(NREC)
FROM ARCHIVE
GROUP BY 1

Optionally combined with a condition for the right date range? (可选)结合正确日期范围的条件?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM