date_add() 增加
MYSQL 获取当前时间加上一个月
update user set leverstart=now(),leverover=date_add(NOW(), interval 1 MONTH) where id=1;
date_sub()减少
date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second)
month 月份
minute 分钟
second 秒
hour 小时
week 周
quarter 刻
year 年
获得当前时间:now();sysdate()
获得当前时间戳函数:current_timestamp, current_timestamp()
日期/时间 转换为字符串函数:date_format(date,format), time_format(time,format)
字符串转换为日期函数:str_to_date(str, format)
日期、天数 转换函数:to_days(date), from_days(days)
时间、秒 转换函数:time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:00'); -- 3600
select sec_to_time(3600); -- '01:00:00'
拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
select makedate(1901,30); -- '1901-01-30'
select makedate(1901,33); -- '2001-02-02'
select maketime(11,35,30); -- '11:35:30'
日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
select datediff('2008-08-01', '2008-08-08'); -- -7
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08
时间戳(timestamp)转换、增、减函数:
timestamp(date) -- date to timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01
select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00
timestampadd() 函数
select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1
select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12
select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7
timestampdiff() 函数就比 datediff() 功能强,datediff() 只能计算两个日期(date)之间相差的天数。