now (); 当前具体的日期和时间

curdate (); 当前日期

curtime(); 当前时间

1.MySQL加减某个时间间隔
设置当前日期变量

set @dt = now();      //设置当前日期

select @dt;    //查询变量值


 

加减某个时间间隔函数date_add()与date_sub()

date_add('某个日期时间',interval 1 时间种类名);

示例:

select date_add(@dt, interval 1 year);    //加1年
select date_add(@dt, interval 1 month);    //加1月

quarter:季,week:周,day:天,hour:小时,minute:分钟,second:秒,microsecond:毫秒

注:也可以不用变量,直接加减某个时间,如:select date_add('1998-01-01', interval 1 day);

2.日期相减
datediff(date1,date2):两个日期相减,date1减去date2得到相减之后的天数


timediff(time1,time2):两个时间相减 time1减time2,返回差值。


 

select timediff('2019-06-03 12:30:00', '2019-06-03 12:29:30');

等同于

select timediff('12:30:00', '12:29:30');

原文链接:https://blog.csdn/qq_39588003/article/details/90758827

更多推荐

MySQL 日期时间加减