MySQL 日期时间加减
now (); 当前具体的日期和时间
curdate (); 当前日期
curtime(); 当前时间
一、MySQL加减某个时间间隔 :adddate(), addtime()函数,可以用date_add() 来替代。
1.语法: date_add(‘某个日期时间’,interval 1 时间种类名);
2.设置当前日期变量
set @dt = now(); //设置当前日期
select @dt; //查询变量值
quarter:季,week:周,day:天,hour:小时,minute:分钟,second:秒,microsecond:毫秒
3.示例:
select date_add(now(), interval 1 day); - 加1天
select date_add(now(), interval 1 hour); -加1小时
select date_add(now(), interval 1 minute); - 加1分钟
select date_add(now(), interval 1 second); -加1秒
select date_add(now(), interval 1 microsecond);-加1毫秒
select date_add(now(), interval 1 week);-加1周
select date_add(now(), interval 1 month);-加1月
select date_add(now(), interval 1 quarter);-加1季
select date_add(now(), interval 1 year);-加1年
注:也可以不用变量,直接加减某个时间,如:select date_add(‘1998-01-01’, interval 1 day);
二、.日期相减
1.MySQL date_sub() 日期时间函数 和date_add() 用法一致。
2.MySQL 中subdate(),subtime()函数,建议,用date_sub()来替代。
3.datediff(date1,date2):两个日期相减,date1减去date2得到相减之后的天数
4.timediff(time1,time2):两个时间相减 time1减time2,返回差值。
5.示例:
select timediff(‘2019-06-0312:30:00’, ‘2019-06-0312:29:30’);
等同于
select timediff(‘12:30:00’, ‘12:29:30’);
原文链接:http://www.jxszl.com/biancheng/shujuku/84285.html