获得当前日期、时间函数
mysql> select curdate(); +------------+
| curdate() | +------------+
| 2015-08-06 | +------------+
1 row in set (0.00 sec)
mysql> select current_date(); +----------------+
| current_date() | +----------------+
| 2015-08-06 | +----------------+
1 row in set (0.00 sec)
mysql> select curtime(); +-----------+
| curtime() | +-----------+
| 12:01:48 | +-----------+
1 row in set (0.00 sec)
mysql> select current_time(); +----------------+
| current_time() | +----------------+
| 12:01:32 | +----------------+
1 row in set (0.00 sec)
mysql> select now(); +---------------------+
| now() | +---------------------+
| 2015-08-06 11:26:49 | +---------------------+
1 row in set (0.00 sec)
mysql> select sysdate(); +---------------------+
| sysdate() | +---------------------+
| 2015-08-06 11:26:55 | +---------------------+
1 row in set (0.00 sec)
注:now()在语句开始执行时值就得到了, 而sysdate()是在语句执行时动态得到值,具体实例如下:
mysql> select now(), sleep(3), now(); +---------------------+----------+---------------------+
| now() | sleep(3) | now() | +---------------------+----------+---------------------+
| 2015-08-06 11:32:17 | 0 | 2015-08-06 11:32:17 | +---------------------+----------+---------------------+
1 row in set (3.00 sec)
mysql> select sysdate(), sleep(3), sysdate(); +---------------------+----------+---------------------+
| sysdate() | sleep(3) | sysdate() | +---------------------+----------+---------------------+
| 2015-08-06 11:32:57 | 0 | 2015-08-06 11:33:00 | +---------------------+----------+---------------------+
1 row in set (3.00 sec)
mysql> select weekday('2015-08-11'); +-----------------------+
| weekday('2015-08-11') | +-----------------------+
| 1 | +-----------------------+
1 row in set (0.00 sec)
mysql> select weekday('2015-08-11 11:18:00'); +--------------------------------+
| weekday('2015-08-11 11:18:00') | +--------------------------------+
| 1 | +--------------------------------+
1 row in set (0.00 sec)
mysql> select dayofmonth('2015-08-11 11:18:00'); +-----------------------------------+
| dayofmonth('2015-08-11 11:18:00') | +-----------------------------------+
| 11 | +-----------------------------------+
1 row in set (0.00 sec)
mysql> select dayofyear('2015-08-11 11:18:00'); +----------------------------------+
| dayofyear('2015-08-11 11:18:00') | +----------------------------------+
| 223 | +----------------------------------+
1 row in set (0.00 sec)
mysql> select year('2015-08-11 11:18:00'); +-----------------------------+
| year('2015-08-11 11:18:00') | +-----------------------------+
| 2015 | +-----------------------------+
1 row in set (0.00 sec)
mysql> select month('2015-08-11 11:18:00'); +------------------------------+
| month('2015-08-11 11:18:00') | +------------------------------+
| 8 | +------------------------------+
1 row in set (0.00 sec)
mysql> select day('2015-08-11 11:18:00'); +----------------------------+
| day('2015-08-11 11:18:00') | +----------------------------+
| 11 | +----------------------------+
1 row in set (0.00 sec)
mysql> select dayname('2015-08-11 11:18:00'); +--------------------------------+
| dayname('2015-08-11 11:18:00') | +--------------------------------+
| Tuesday | +--------------------------------+
1 row in set (0.00 sec)
mysql> select monthname('2015-08-11 11:18:00'); +----------------------------------+
| monthname('2015-08-11 11:18:00') | +----------------------------------+
| August | +----------------------------------+
1 row in set (0.00 sec)
mysql> select quarter('2015-08-11 11:18:00'); +--------------------------------+
| quarter('2015-08-11 11:18:00') | +--------------------------------+
| 3 | +--------------------------------+
1 row in set (0.00 sec)
mysql> select week('2015-08-11 11:18:00'); +-----------------------------+
| week('2015-08-11 11:18:00') | +-----------------------------+
| 32 | +-----------------------------+
1 row in set (0.00 sec)
mysql> select hour('2015-08-11 13:18:00'); +-----------------------------+
| hour('2015-08-11 13:18:00') | +-----------------------------+
| 13 | +-----------------------------+
1 row in set (0.00 sec)
mysql> select minute('2015-08-11 13:18:00'); +-------------------------------+
| minute('2015-08-11 13:18:00') | +-------------------------------+
| 18 | +-------------------------------+
1 row in set (0.00 sec)
mysql> select second('2015-08-11 13:18:50'); +-------------------------------+
| second('2015-08-11 13:18:50') | +-------------------------------+
| 50 | +-------------------------------+
1 row in set (0.00 sec)
日期、时间格式化函数
mysql> select date_format('2015-08-11 13:18:50','%Y%m%d%H%i%s'); +---------------------------------------------------+
| date_format('2015-08-11 13:18:50','%Y%m%d%H%i%s') | +---------------------------------------------------+
| 20150811131850 | +---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_format('20150811131850','%Y-%m-%d %H:%m:%s'); +---------------------------------------------------+
| date_format('20150811131850','%Y-%m-%d %H:%m:%s') | +---------------------------------------------------+
| 2015-08-11 13:08:50 | +---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select time_format('131850','%H:%m:%s'); +----------------------------------+
| time_format('131850','%H:%m:%s') | +----------------------------------+
| 13:00:50 | +----------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08/11/2015','%m/%d/%Y'); +--------------------------------------+
| str_to_date('08/11/2015','%m/%d/%Y') | +--------------------------------------+
| 2015-08-11 | +--------------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08/11/15','%m/%d/%Y'); +------------------------------------+
| str_to_date('08/11/15','%m/%d/%Y') | +------------------------------------+
| 2015-08-11 | +------------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08.11.15','%m.%d.%Y'); +------------------------------------+
| str_to_date('08.11.15','%m.%d.%Y') | +------------------------------------+
| 2015-08-11 | +------------------------------------+
1 row in set (0.00 sec)
mysql> select str_to_date('08.11.2015 12:40:50','%m.%d.%Y %h:%i:%s'); +--------------------------------------------------------+
| str_to_date('08.11.2015 12:40:50','%m.%d.%Y %h:%i:%s') | +--------------------------------------------------------+
| 2015-08-11 00:40:50 | +--------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select to_days('2015-08-11'); +-----------------------+
| to_days('2015-08-11') | +-----------------------+
| 736186 | +-----------------------+
1 row in set (0.00 sec)
mysql> select time_to_sec('12:48:50'); +-------------------------+
| time_to_sec('12:48:50') | +-------------------------+
| 46130 | +-------------------------+
1 row in set (0.00 sec)
mysql> select sec_to_time('46130'); +----------------------+
| sec_to_time('46130') | +----------------------+
| 12:48:50 | +----------------------+
1 row in set (0.00 sec)
mysql> select makedate(2015,31); +-------------------+
| makedate(2015,31) | +-------------------+
| 2015-01-31 | +-------------------+
1 row in set (0.00 sec)
mysql> select maketime(12,15,30); +--------------------+
| maketime(12,15,30) | +--------------------+
| 12:15:30 | +--------------------+
1 row in set (0.00 sec)
日期、时间计算函数
DATE_ADD(date,INTERVAL expr type)
DATE_SUB(date,INTERVAL expr type)
- date是一个 DATETIME 或DATE值,用来指定起始时间。
- expr是表达式,用来指定从起始日期添加或减去的时间间隔值。Expr是字符串;对于负值的时间间隔,它可以以一个 '-'开头。
- type 为关键词,它指示了表达式被解释的方式。
- 关键词INTERVA及type分类符均不区分大小写。
- MySQL允许任何expr格式中的标点分隔符。若 date 参数是一个 DATE 值,而你的计算只会包括 YEAR、MONTH和DAY部分(即没有时间部分), 其结果是一个DATE值,否则结果将是一个DATETIME值。
mysql> select date_add('2014-12-31 23:59:59',INTERVAL 1 SECOND); +---------------------------------------------------+
| date_add('2014-12-31 23:59:59',INTERVAL 1 SECOND) | +---------------------------------------------------+
| 2015-01-01 00:00:00 | +---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2014-12-31 23:59:59',INTERVAL 1 DAY); +------------------------------------------------+
| date_add('2014-12-31 23:59:59',INTERVAL 1 DAY) | +------------------------------------------------+
| 2015-01-01 23:59:59 | +------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2014-12-31 00:00:00',INTERVAL '-1 10' DAY_HOUR); +-----------------------------------------------------------+
| date_add('2014-12-31 00:00:00',INTERVAL '-1 10' DAY_HOUR) | +-----------------------------------------------------------+
| 2014-12-29 14:00:00 | +-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2014-12-31',INTERVAL 31 DAY); +----------------------------------------+
| date_sub('2014-12-31',INTERVAL 31 DAY) | +----------------------------------------+
| 2014-11-30 | +----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2014-12-31',INTERVAL -1 DAY); +----------------------------------------+
| date_sub('2014-12-31',INTERVAL -1 DAY) | +----------------------------------------+
| 2015-01-01 | +----------------------------------------+
1 row in set (0.00 sec)
日期、时间相减函数datediff(date1,date2)和timediff(time1,time2)
mysql> select datediff('2015-08-11','2015-07-11'); +-------------------------------------+
| datediff('2015-08-11','2015-07-11') | +-------------------------------------+
| 31 | +-------------------------------------+
1 row in set (0.00 sec)
mysql> select datediff('2015-05-11','2015-07-11'); +-------------------------------------+
| datediff('2015-05-11','2015-07-11') | +-------------------------------------+
| -61 | +-------------------------------------+
1 row in set (0.00 sec)
mysql> select timediff('2015-08-11 12:12:12','2015-07-11 00:00:00'); +-------------------------------------------------------+
| timediff('2015-08-11 12:12:12','2015-07-11 00:00:00') | +-------------------------------------------------------+
| 756:12:12 | +-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select timediff('2015-08-10 12:12:12','2015-08-11 00:00:00'); +-------------------------------------------------------+
| timediff('2015-08-10 12:12:12','2015-08-11 00:00:00') | +-------------------------------------------------------+
| -11:47:48 | +-------------------------------------------------------+
1 row in set (0.00 sec)
时间戳(timestamp)转换、增、减函数
mysql> select timestamp('2015-08-11'); +-------------------------+
| timestamp('2015-08-11') | +-------------------------+
| 2015-08-11 00:00:00 | +-------------------------+
1 row in set (0.00 sec)
mysql> select timestamp('2015-08-11 00:00:00','01:01:01'); +---------------------------------------------+
| timestamp('2015-08-11 00:00:00','01:01:01') | +---------------------------------------------+
| 2015-08-11 01:01:01 | +---------------------------------------------+
1 row in set (0.00 sec)
mysql> select timestampadd(day,1,'2015-08-11 12:00:00'); +-------------------------------------------+
| timestampadd(day,1,'2015-08-11 12:00:00') | +-------------------------------------------+
| 2015-08-12 12:00:00 | +-------------------------------------------+
1 row in set (0.00 sec)
mysql> select timestampdiff(year,'2015-08-11','2014-08-11'); +-----------------------------------------------+
| timestampdiff(year,'2015-08-11','2014-08-11') | +-----------------------------------------------+
| -1 | +-----------------------------------------------+
1 row in set (0.00 sec)
mysql> select timestampdiff(day,'2015-08-11','2014-07-11'); +----------------------------------------------+
| timestampdiff(day,'2015-08-11','2014-07-11') | +----------------------------------------------+
| -396 | +----------------------------------------------+
1 row in set (0.00 sec)