MySQL的时间差函数(
1. 时间差函数(TIMESTAMPDIFF、DATEDIFF)
需要⽤MySQL计算时间差,使⽤TIMESTAMPDIFF、DATEDIFF,记录⼀下实验结果
--0
select datediff(now(), now());
--2
select datediff('2015-04-22 23:59:00', '2015-04-20 00:00:00');
--2
select datediff('2015-04-22 00:00:00', '2015-04-20 23:59:00');
--1
select TIMESTAMPDIFF(DAY, '2015-04-20 23:59:00', '2015-04-22 00:00:00');
-
-2
select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 00:00:00');
--2
select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 12:00:00');
--2
select TIMESTAMPDIFF(DAY, '2015-04-20 00:00:00', '2015-04-22 23:59:00');
--71
select TIMESTAMPDIFF(HOUR, '2015-04-20 00:00:00', '2015-04-22 23:00:00');
--4260
select TIMESTAMPDIFF(MINUTE, '2015-04-20 00:00:00', '2015-04-22 23:00:00');
2. ⽇期转换计算函数(date_add、day、date_format、str_to_date)
-
- ⽤⽇期与字符串转换,计算当⽉第⼀天、下⽉第⼀天
select curdate() as '当前⽇期',
DATE_FORMAT(curdate(), '%Y-%m') as '当前⽉份',
str_to_date(concat(DATE_FORMAT(curdate(), '%Y-%m'), '-01'), '%Y-%m-%d') as '当前⽉的第⼀天',
date_add(str_to_date(concat(DATE_FORMAT(curdate(), '%Y-%m'), '-01'), '%Y-%m-%d'), interval 1 month) as '下⽉的第⼀天'; -- 当前⽉的最后⼀天
select last_day(curdate());
-- 下⽉第⼀天
diff函数select date_add(last_day(curdate()), interval 1 day);
-- 当天为当⽉的第⼏天
select day(curdate());
-- 当⽉第⼀天
select date_add(curdate(), interval 1-(day(curdate())) day);
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论