我需要天,小时和分钟两个日期之间的差异。当前日期(NOW())和end_date。我需要在两个单独的查询中使用它们,这些查询的工作方式略有不同。
第一个需要以下列格式将它们作为单个列返回:
-------------------------
| time_left |
-------------------------
| x days x hours x min |
-------------------------
第二个需要在三个单独的列中返回它们,如果end_date超过当前日期,则确保每列返回0:
-------------------------------------------
| days_left | hrs_left | min_left |
-------------------------------------------
| x | x | x |
-------------------------------------------
我有一个解决方案,但这很可怕。我敢肯定有人找到了至少一种解决这个问题的相对简洁的方法。谢谢你!
答案 0 :(得分:2)
之前的那个是不准确的,在一小时应该显示一天三小时的差异。
任何时候都应该准确
SELECT
@diff:=ABS( UNIX_TIMESTAMP("2014-05-09 21:24:25") - UNIX_TIMESTAMP() ) ,
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(@diff-@minutes*60 AS SIGNED) as seconds;
说明:
答案 1 :(得分:0)
我使用了几年,几个月和几天,但是可以对其进行编辑以包括更多内容:
concat(timestampdiff(YEAR,'2020-01-01 11:00:00','2020-04-04 13:22:45'), ' years, ',
timestampdiff(MONTH,'2020-01-01 11:00:00','2020-04-04 13:22:45' - INTERVAL timestampdiff(YEAR,'2020-01-01 11:00:00','2020-04-04 13:22:45') YEAR),' months, ',
timestampdiff(DAY,'2020-01-01 11:00:00','2020-04-04 13:22:45' - INTERVAL timestampdiff(YEAR,'2020-01-01 11:00:00','2020-04-04 13:22:45') YEAR - INTERVAL timestampdiff(MONTH,'2020-01-01 11:00:00','2020-04-04 13:22:45' - INTERVAL timestampdiff(YEAR,'2020-01-01 11:00:00','2020-04-04 13:22:45') YEAR) MONTH),' days'),
答案 2 :(得分:0)
select CONCAT( FLOOR(HOUR(TIMEDIFF('2020-01-01 11:00:00','2020-04-04 13:22:45')) / 24), ' DAYS ',MOD(HOUR(TIMEDIFF('2020-01-01 11:00:00','2020-04-04 13:22:45')), 24), ' HOURS ',
MINUTE(TIMEDIFF('2020-01-01 11:00:00','2020-04-04 13:22:45')), ' MINUTES ') AS date_year;
您可以使用您的专栏代替提到的日期。输出在下面提到
date_year
34 DAYS 22 HOURS 59 MINUTES