標籤:
mysql時間處理
通常我們會有一些時間的轉換需求,比如要統計某個時間段的收入,比如要截取某個時間的年份,比如要根據某個日期推算出是星期幾等個,這些都可以通過Mysql內建的時間函數很容易實現。因為我對Mysql的函數還不熟,而通常又會調用這些時間函數,這邊稍加總結以便查詢。
–返回目前時間
mysql> select now(),date(now()),sysdate();
+---------------------+-------------+---------------------+
| now() | date(now()) | sysdate() |
+---------------------+-------------+---------------------+
| 2008-12-02 10:11:36 | 2008-12-02 | 2008-12-02 10:11:36 |
+---------------------+-------------+---------------------+
1 row in set (0.00 sec)
mysql> select curdate(),curdate()+0,curtime(),curtime()+0;
+---------------------+-------------+---------------------+
| curdate() | curdate()+0 | curtime() | curtime()+0 |
+---------------------+-------------+---------------------+
| 2008-12-02 | 20081202 | 10:00:33 | 100033.000000 |
+---------------------+-------------+---------------------+
–返回日期當月最後一天
mysql> select last_day(‘2008-12-02‘);
+------------------------+
| last_day(‘2008-12-02‘) |
+------------------------+
| 2008-12-31 |
+------------------------+
1 row in set (0.00 sec)
–返回日期的星期幾
mysql> select dayname(‘2008-12-02‘),dayofweek(‘2008-12-02‘);
+-----------------------+-------------------------+
| dayname(‘2008-12-02‘) | dayofweek(‘2008-12-02‘) |
+-----------------------+-------------------------+
| tuesday | 3 |
+-----------------------+-------------------------+
1 row in set (0.00 sec)
–返回日期的年,月,日
mysql> select month(‘2008-12-02‘),year(‘2008-12-02‘),day(‘2008-12-02‘);
+---------------------+--------------------+-------------------+
| month(‘2008-12-02‘) | year(‘2008-12-02‘) | day(‘2008-12-02‘) |
+---------------------+--------------------+-------------------+
| 12 | 2008 | 2 |
+---------------------+--------------------+-------------------+
1 row in set (0.00 sec)
–返回日期的小時,分,秒
mysql> select hour(‘10:05:03‘),minute(‘10:05:03‘),second(‘10:05:03‘);
+------------------+--------------------+--------------------+
| hour(‘10:05:03‘) | minute(‘10:05:03‘) | second(‘10:05:03‘) |
+------------------+--------------------+--------------------+
| 10 | 5 | 3 |
+------------------+--------------------+--------------------+
1 row in set (0.00 sec)
1.subdate(d,t):起始時間加上一段時間(year,month,day…)
mysql> select date_add(‘1998-01-02‘, interval 31 day),adddate(‘1998-01-02‘, 31);
+-----------------------------------------+---------------------------+
| date_add(‘1998-01-02‘, interval 31 day) | adddate(‘1998-01-02‘, 31) |
+-----------------------------------------+---------------------------+
| 1998-02-02 | 1998-02-02 |
+-----------------------------------------+---------------------------+
1 row in set (0.00 sec)
mysql> select date_add(‘1998-01-02‘,interval 2 year);
+-----------------------------------------------------+
| date_add(‘1998-01-02‘, interval 2 year)
+-----------------------------------------------------+
| 2000-01-02
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add(‘1998-01-02‘, interval 2 hour);
+-----------------------------------------------------+
| date_add(‘1998-01-02‘, interval 2 hour)
+-----------------------------------------------------+
| 1998-01-02 02:00:00
+-----------------------------------------------------+
1 row in set (0.00 sec)
2.subdate(d,t):起始時間減去一段時間
mysql> select subdate(‘1998-01-02‘, interval 31 day),subdate(‘1998-01-02‘, 31);
+----------------------------------------+---------------------------+
| subdate(‘1998-01-02‘, interval 31 day) | subdate(‘1998-01-02‘, 31) |
+----------------------------------------+---------------------------+
| 1997-12-02 | 1997-12-02 |
+----------------------------------------+---------------------------+
1 row in set (0.00 sec)
3.addtime(d,t):起始時間d加入時間t
mysql> select addtime(‘1997-12-31 23:59:50‘,‘00:00:05‘), addtime(‘23:59:50‘,‘00:00:05‘) ;
+-------------------------------------------+--------------------------------+
| addtime(‘1997-12-31 23:59:50‘,‘00:00:05‘) | addtime(‘23:59:50‘,‘00:00:05‘) |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:55 | 23:59:55 |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)
4.subtime(d,t):起始時間d減去時間t
mysql> select subtime(‘1997-12-31 23:59:50‘,‘00:00:05‘), subtime(‘23:59:50‘,‘00:00:05‘);
+-------------------------------------------+--------------------------------+
| subtime(‘1997-12-31 23:59:50‘,‘00:00:05‘) | subtime(‘23:59:50‘,‘00:00:05‘) |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:45 | 23:59:45 |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)
5.datediff(d1,d2):返回起始時間d1和結束時間d2之間的天數
mysql> select datediff(‘1997-12-31 23:59:59‘,‘1997-12-30‘);
+----------------------------------------------+
| datediff(‘1997-12-31 23:59:59‘,‘1997-12-30‘) |
+----------------------------------------------+
| 1 |
+----------------------------------------------+
1 row in set (0.00 sec)
6.date_format(date,format):根據format字串顯示date值的格式
mysql> select date_format(‘2008-12-02 22:23:00‘, ‘%y %m %m %h:%i:%s‘);
+---------------------------------------------------------+
| date_format(‘2008-12-02 22:23:00‘, ‘%y %m %m %h:%i:%s‘) |
+---------------------------------------------------------+
| 2008 12 12 22:23:00 |
+---------------------------------------------------------+
1 row in set (0.00 sec)
7.str_to_date(str,format) 字串轉化為時間
mysql> select str_to_date(‘04/31/2004‘, ‘%m/%d/%y %h:%i:s‘);
+-----------------------------------------------+
| str_to_date(‘04/31/2004‘, ‘%m/%d/%y %h:%i:s‘) |
+-----------------------------------------------+
| 2004-04-31 00:00:00 |
+-----------------------------------------------+
1 row in set (0.00 sec)
8.timestamp(expr) , timestamp(expr,expr2) :
對於一個單參數,該函數將日期或日期時間運算式 expr 作為日期時間值返回.對於兩個參數, 它將時間運算式 expr2添加到日期或日期時間運算式 expr 中,將theresult作為日期時間值返回
mysql> select timestamp(‘2003-12-31‘), timestamp(‘2003-12-31 12:00:00‘,‘12:00:00‘);
+-------------------------+---------------------------------------------+
| timestamp(‘2003-12-31‘) | timestamp(‘2003-12-31 12:00:00‘,‘12:00:00‘) |
+-------------------------+---------------------------------------------+
| 2003-12-31 00:00:00 | 2004-01-01 00:00:00 |
+-------------------------+---------------------------------------------+
1 row in set (0.00 sec)
9.取當天0點0分,下一天0點0分
mysql> select timestamp(date(sysdate())),timestamp(adddate(date(sysdate()),1));
+----------------------------+---------------------------------------+
| timestamp(date(sysdate())) | timestamp(adddate(date(sysdate()),1)) |
+----------------------------+---------------------------------------+
| 2008-12-02 00:00:00 | 2008-12-03 00:00:00 |
+----------------------------+---------------------------------------+
1 row in set (0.00 sec)
【Mysql】時間處理