###My SQL Date相关函数 #获得当前日期时间 select now(); #date_format(date,format) select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s'); #str_to_date(str, format) select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09 select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09 select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09 select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30 select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30 #makdedate(year,dayofyear), maketime(hour,minute,second) select makedate(2001,31); -- '2001-01-31' select makedate(2001,32); -- '2001-02-01' select maketime(12,15,30); -- '12:15:30' select unix_timestamp(); -- 1218290027 select from_unixtime(1218290027); -- '2008-08-09 21:53:47' #date_add() set @dt = now(); select date_add(@dt, interval 1 day); -- add 1 day select date_add(@dt, interval 1 hour); -- add 1 hour select date_add(@dt, interval 1 minute); -- ... select date_add(@dt, interval 1 second); select date_add(@dt, interval 1 microsecond); select date_add(@dt, interval 1 week); select date_add(@dt, interval 1 month); select date_add(@dt, interval 1 quarter); select date_add(@dt, interval 1 year); #addtime set @dt = '2008-08-09 12:12:33'; select date_add(@dt, interval '01:15:30' hour_second); #date_sub select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second); #datediff(date1,date2):两个日期相减 date1 - date2,返回天数。 select datediff('2008-08-08', '2008-08-01'); -- 7 select datediff('2008-08-01', '2008-08-08'); -- -7 #timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。 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 #timestampadd() 函数类似于 date_add()。 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 #https://www.cnblogs.com/php12-cn/p/8882221.html