下面本文章给各位整理了不少的mysql时间日期函数例子了,下面我们一起来看看这些日期时间函数的一些应用例子,希望文章对各位会有帮助.
1.DAYOFWEEK(date);返回data时间是周几(1 => 星期日,2 => 星期一,3 => 星期二,……,7 => 星期六).
- mysql>selectDAYOFWEEK('2013-06-0914:00:00');
- +----------------------------------+
- |DAYOFWEEK('2013-06-0914:00:00')|
- +----------------------------------+
- |1|
- +----------------------------------+
2.DAYOFMONTH(date);返回date是一个月的第几天 1——31
- mysql>selectDAYOFMONTH('2013-06-0914:00:00');
- +-----------------------------------+
- |DAYOFMONTH('2013-06-0914:00:00')|
- +-----------------------------------+
- |9|
- +-----------------------------------+
3.DAYOFYEAR(date);返回date是一年中的第几天 1———366
- mysql>selectDAYOFYEAR('2013-06-0914:00:00');
- +----------------------------------+
- |DAYOFYEAR('2013-06-0914:00:00')|
- +----------------------------------+
- |160|
- +----------------------------------+
4.WEEKDAY(date);返回date是周几(0 => 星期一,1 => 星期二,2 => 星期三,……, 6=> 星期日).
- mysql>selectWEEKDAY('2013-06-0914:00:00');
- +--------------------------------+
- |WEEKDAY('2013-06-0914:00:00')|
- +--------------------------------+
- |6|
- +--------------------------------+
5.MONTH(date);返回date的月份 1—–12
- mysql>selectMONTH('2013-06-0914:00:00');
- +------------------------------+
- |MONTH('2013-06-0914:00:00')|
- +------------------------------+
- |6|
- +------------------------------+
6.DAYNAME(date);返回date的星期英文名
- mysql>selectDAYNAME('2013-06-0914:00:00');
- +--------------------------------+
- |DAYNAME('2013-06-0914:00:00')|
- +--------------------------------+
- |Sunday|
- +--------------------------------+
7.MONTHNAME(date);返回date的月份的英文名
- mysql>selectMONTHNAME('2013-06-0914:00:00');
- +----------------------------------+
- |MONTHNAME('2013-06-0914:00:00')|
- +----------------------------------+
- |June|
- +----------------------------------+
8.QUARTER(date);返回date在季度中的排序 1——-4
- mysql>selectQUARTER('2013-06-0914:00:00');
- +--------------------------------+
- |QUARTER('2013-06-0914:00:00')|
- +--------------------------------+
- |2|
- +--------------------------------+
9.WEEK(date,first);返回date是一年的第几个周,first = 0从星期日开始算,first = 1从星期一开始算 默认为00——–52
- mysql>selectWEEK('2013-06-0914:00:00',0);
- +-------------------------------+
- |WEEK('2013-06-0914:00:00',0)|
- +-------------------------------+
- |23|
- +-------------------------------+
10.HOUR(date);返回date的小时部分
- mysql>selectHOUR('2013-06-0914:00:00');
- +-----------------------------+
- |HOUR('2013-06-0914:00:00')|
- +-----------------------------+
- |14|
- +-----------------------------+
11.YEAR(date);返回date的年份部分
- mysql>selectYEAR('2013-06-0914:00:00');
- +-----------------------------+
- |YEAR('2013-06-0914:00:00')|
- +-----------------------------+
- |2013|
- +-----------------------------+
12.MINUTE(date);返回date的分钟部分
- mysql>selectMINUTE('2013-06-0914:22:22');
- +-------------------------------+
- |MINUTE('2013-06-0914:22:22')|
- +-------------------------------+
- |22|
- +-------------------------------+
13.SECOND(date);返回date的秒部分
- mysql>selectSECOND('2013-06-0914:22:22');
- +-------------------------------+
- |SECOND('2013-06-0914:22:22')|
- +-------------------------------+
- |22|
- +-------------------------------+
14.PERIOD_ADD(date,num);date加上num后的日期 date的日期格式为 yyyymmdd 或者yyyymm,若精确到日num单位是日,若是月则num为月单位增加
- mysql>selectPERIOD_ADD(201306,3);
- +----------------------+
- |PERIOD_ADD(201306,3)|
- +----------------------+
- |201309|
- +----------------------+
- mysql>selectPERIOD_ADD(20130609,3);
- +------------------------+
- |PERIOD_ADD(20130609,3)|
- +------------------------+
- |20130612|
- +------------------------+
15.PERIOD_DIFF(date1,date2);date1减去date2的差值月数
- mysql>selectPERIOD_DIFF(201306,201309);
- +----------------------------+
- |PERIOD_DIFF(201306,201309)|
- +----------------------------+
- |-3|
- +----------------------------+
16.DATE_ADD(date, INTERVAL num type) == ADDDATE(date, INTERVAL num type);DATE_SUB(date, INTERVAL num type) == SUBDATE(date, INTERVAL num type);date相加或者相减一个制定的单位时间.
type的值:SECOND,MINUTE,HOUR,DAY,MONTH,WEEK,MONTH,YEAR
- mysql>selectDATE_ADD('2013-06-0914:22:22',INTERVAL1DAY);
- +------------------------------------------------+
- |DATE_ADD('2013-06-0914:22:22',INTERVAL1DAY)|
- +------------------------------------------------+
- |2013-06-1014:22:22|
- +------------------------------------------------+
- mysql>selectADDDATE('2013-06-0914:22:22',INTERVAL1DAY);
- +-----------------------------------------------+
- |ADDDATE('2013-06-0914:22:22',INTERVAL1DAY)|
- +-----------------------------------------------+
- |2013-06-1014:22:22|
- +-----------------------------------------------+
16.TO_DAYS(date);给定一个日期返回一个天数(从0年开始的天数)
- mysql>selectTO_DAYS('2013-06-0914:22:22');
- +--------------------------------+
- |TO_DAYS('2013-06-0914:22:22')|
- +--------------------------------+
- |735393|
- +--------------------------------+
17.FROM_DAYS(num);给定一个天数 返回一个日期
- mysql>selectFROM_DAYS(752341);
- +-------------------+
- |FROM_DAYS(752341)|
- +-------------------+
- |2059-11-03|
- +-------------------+
18.DATE_FORMAT(date,format);格式化日期