无法正确计算时差

时间:2020-05-18 10:27:25

标签: php mysql sql

我认为查询中存在一些格式问题,由于该原因,我无法计算TIMEDIFF()。当将硬编码日期直接输入到查询中时,例如2016-09-10,它可以工作。但是,当我将日期存储在变量中时,它将不起作用。在MySQL中,日期已经存储在TimeStamp数据类型字段中。请检查下面的代码。

$from_date=date_format(date_create("2020-04-02 17:49:5"),"Y-m-d");
$to_date=date_format(date_create("2020-08-08 18:49:5"),"Y-m-d");
SELECT A1.u_id_fk, 
       A3.full_name, 
       A1.created AS check_in_at, 
       A2.created AS check_out_at, 
       TIMEDIFF(A2.created, A1.created) AS total_time 
FROM timesheet AS A1 
INNER JOIN timesheet AS A2 ON A1.u_id_fk = A2.u_id_fk 
                          AND DATE(A1.created) = DATE(A2.created) 
INNER JOIN users A3 on A1.u_id_fk=A3.u_id 
WHERE 1 = 1 
  AND A1.status = 'Check-In' 
  AND A2.status = 'Check-Out' 
  AND DATE(A1.created) BETWEEN :todate AND :fromdate 
  AND DATE(A2.created) BETWEEN :todate AND :fromdate 
ORDER BY A1.created DESC
$user_login->execute(["todate"=>$to_date,"fromdate"=>$from_Date]); 

我希望输出结果如下,以Y-m-d格式给出日期和日期来过滤结果

结果

u_id_fk         check_in_at            check_out_at           total_time

EMP0002         2016-08-21 14:52:48    2016-08-21 21:09:18    06:16:30
EMP0001         2016-08-20 09:30:30    2016-08-20 18:15:00    08:44:30

0 个答案:

没有答案