1. 程式人生 > >mysql 日期 格式化 加減

mysql 日期 格式化 加減

DATE_ADD(date,INTERVAL expr type)  (增加一段時間)
DATE_SUB(date,INTERVAL expr type)  (減去一段時間)
SELECT DATE_ADD('2014-08-08',INTERVAL 24*3600-1 SECOND) time; (注意 date  '2014-08' 格式錯誤)

(SELECT 
date_format(date_add(now(),interval 0 year), '%Y') code,
CONCAT(date_format(date_add(now(),interval 0 year), '%Y'),'年') name)  
UNION
(SELECT 
date_format(date_add(now(),interval -1 MONTH), '%Y-%m') code,
CONCAT(date_format(date_add(now(),interval -1 year), '%Y'),'年') name) 
UNION
(SELECT 
date_format(date_add(now(),interval -2 year), '%Y') code,
CONCAT(date_format(date_add(now(),interval -2 year), '%Y'),'年') name) 
UNION
(SELECT 
date_format(date_add(now(),interval -3 year), '%Y') code,
CONCAT(date_format(date_add(now(),interval -3 year), '%Y'),'年') name) 
UNION
(SELECT 
date_format(date_add(now(),interval -4 year), '%Y') code,
CONCAT(date_format(date_add(now(),interval -4 year), '%Y'),'年') name) 

結果: code    name

              2015   2015年

              2015-08  2014年

             。。。。。