MySql计算时间差值,天小时分钟秒。

SELECT 
    @diff:=ABS( UNIX_TIMESTAMP("2023-06-09 21:24:25") - UNIX_TIMESTAMP() ) AS stamp, 
    CAST(@days := IF(@diff/86400 >= 1, floor(@diff / 86400 ),0) AS SIGNED) AS days, 
    CAST(@hours := IF(@diff/3600 >= 1, floor((@diff:=@diff-@days*86400) / 3600),0) AS SIGNED) AS hours, 
    CAST(@minutes := IF(@diff/60 >= 1, floor((@diff:=@diff-@hours*3600) / 60),0) AS SIGNED) AS minutes, 
    CAST(@seconds :=@diff-@minutes*60 AS SIGNED) as seconds,
    CONCAT(@days ,'天', @hours,'小时', @minutes, '分钟', @seconds, '秒') 

注意

ConnectionStrings字符串中需增加 AllowUserVariables=True,否则程序调用会出错。