为每2行计算时间戳之间的时间间隔

x759pob2  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(405)

我有下面的表格,正如你在表格中看到的,一个用户有一个break-out和break-in条目,但是我很难计算每个break花费的时间。你们能帮忙吗?谢谢。

ID  Name      Action     Datetime
 2  John Doe  BREAK OUT  2018-05-24 09:00:41
 3  John Doe  BREAK IN   2018-05-24 09:10:45
 4  John Doe  BREAK OUT  2018-05-24 13:00:49
 5  John Doe  BREAK IN   2018-05-24 13:30:52
 6  John Doe  BREAK OUT  2018-05-24 15:30:56
 7  John Doe  BREAK IN   2018-05-24 15:40:59
zazmityj

zazmityj1#

如果你想在mysql中这样做,那么你可以使用这样的子查询。1为员工提供个人id,并将他们的姓名存储在另一个表中,其中员工id是主键,他们的个人详细信息存储在该表中。2在这个表中,只有员工id、休息时间和休息时间以及日期。然后您可以使用此查询并选择一个特定的员工来查找他们的休息时间。

select timediff((select breakout_time from table where employee_id = ? and date=?),(select breakin_time from table where employee_id = "same as above" and date="Same as above")) from dual;

欢迎编辑或更新。希望这有帮助。

balp4ylt

balp4ylt2#

好吧,既然你没有说你是如何得到这些数据的,你想在哪里显示结果的,那么下面就是你如何得到你想要的:

function getDateDiff($breakOut, $breakIn) {
    $dateStart = new DateTime($breakOut);
    $dateEnd   = new DateTime($breakIn); 

    $dateDiff  = $dateStart->diff($dateEnd); 

    return $dateDiff->format("%H:%I:%S");
}

现在您只需使用函数并返回diff:

2   John Doe    BREAK OUT   2018-05-24 09:00:41
3   John Doe    BREAK IN    2018-05-24 09:10:45

var_dump(getDateDiff("2018-05-24 09:00:41", "2018-05-24 09:10:45")); // '00:10:04'

4   John Doe    BREAK OUT   2018-05-24 13:00:49
5   John Doe    BREAK IN    2018-05-24 13:30:52

var_dump(getDateDiff("2018-05-24 13:00:49", "2018-05-24 13:30:52")); // '00:30:03'

6   John Doe    BREAK OUT   2018-05-24 15:30:56
7   John Doe    BREAK IN    2018-05-24 15:40:59

var_dump(getDateDiff("2018-05-24 15:30:56", "2018-05-24 15:40:59")); // '00:10:03'

因此,如果希望在php循环中获得这些结果,可以尝试以下方法:

// your data array, I assume it looks like this since I have no info
$array(
    2 => array(
        "name" => "John Doe",
        "break" => "2018-05-24 09:00:41",
    ),
    3 => array(
        "name" => "John Doe",
        "break" => "2018-05-24 09:10:45",
    ),
    4 => array(
        "name" => "John Doe",
        "break" => "2018-05-24 13:00:49",
    ),
    5 => array(
        "name" => "John Doe",
        "break" => "2018-05-24 13:30:52",
    ),
    ...
); 

$array_break_time = array();
foreach ($array as $key => $data) {
    if ($key % 2 == 0) {
        if(isset($array[$key + 1])) {
            $array_break_time[] = getDateDiff($data["break"], $array[$key + 1]["break"])
        }
    }
}

输出为:

$array_break_time = array(
    0 => '00:10:04',
    1 => '00:30:03'
    ....
);

但因为我没有更多的细节,我不知道这对你来说是否好!

vlurs2pr

vlurs2pr3#

例如。:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(Action VARCHAR(20) NOT NULL,dt DATETIME PRIMARY KEY);

INSERT INTO my_table VALUES
('BREAK OUT','2018-05-24 09:00:41'),
('BREAK IN','2018-05-24 09:10:45'),
('BREAK OUT','2018-05-24 13:00:49'),
('BREAK IN','2018-05-24 13:30:52'),
('BREAK OUT','2018-05-24 15:30:56'),
('BREAK IN','2018-05-24 15:40:59');

SELECT bi.dt
     , SEC_TO_TIME(TIME_TO_SEC(bi.dt)-TIME_TO_SEC(MAX(bo.dt)))x 
  FROM my_table bi 
  JOIN my_table bo 
    ON bo.dt <= bi.dt 
 WHERE bo.action = 'break out' 
   AND bi.action = 'break in' 
 GROUP 
    BY bi.dt;

+---------------------+----------+
| dt                  | x        |
+---------------------+----------+
| 2018-05-24 09:10:45 | 00:10:04 |
| 2018-05-24 13:30:52 | 00:30:03 |
| 2018-05-24 15:40:59 | 00:10:03 |
+---------------------+----------+

相关问题