MySQL數(shù)據(jù)庫(kù)的日期函數(shù)與時(shí)間函數(shù)
以下的文章主要講述的是MySQL數(shù)據(jù)庫(kù)的日期函數(shù)與時(shí)間函數(shù)(MySQL 5.X)的實(shí)際應(yīng)用的經(jīng)驗(yàn)總結(jié),MySQL日期函數(shù)與時(shí)間函數(shù)在實(shí)際應(yīng)用中出現(xiàn)的頻率還是很大的,以下的文章就是對(duì)這兩個(gè)函數(shù)的詳細(xì)描述。
MySQL 數(shù)據(jù)庫(kù)獲得當(dāng)前日期時(shí)間 函數(shù):
1.1 獲得當(dāng)前日期+時(shí)間(date + time)函數(shù):now()
- mysql> select now();
- +---------------------+
- | now() |
- +---------------------+
- | 2008-08-08 22:20:46 |
- +---------------------+
除了 now() 函數(shù)能獲得當(dāng)前的日期時(shí)間外,MySQL 中還有下面的函數(shù):
- current_timestamp()
- ,current_timestamp
- ,localtime()
- ,localtime
- ,localtimestamp -- (v4.0.6)
- ,localtimestamp() -- (v4.0.6)
這些日期時(shí)間函數(shù),都等同于 now()。鑒于 now() 函數(shù)簡(jiǎn)短易記,建議總是使用 now() 來(lái)替代上面列出的函數(shù)。
1.2 獲得當(dāng)前日期+時(shí)間(date + time)函數(shù):sysdate()
sysdate() 日期時(shí)間函數(shù)跟 now() 類似,不同之處在于:now() 在執(zhí)行開始時(shí)值就得到了, sysdate() 在函數(shù)執(zhí)行時(shí)動(dòng)態(tài)得到值。看下面的例子就明白了:
- mysql> select now(), sleep(3), now();
- +---------------------+----------+---------------------+
- | now() | sleep(3) | now() |
- +---------------------+----------+---------------------+
- | 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |
- +---------------------+----------+---------------------+mysql> select sysdate(), sleep(3), sysdate();
- +---------------------+----------+---------------------+
- | sysdate() | sleep(3) | sysdate() |
- +---------------------+----------+---------------------+
- | 2008-08-08 22:28:41 | 0 | 2008-08-08 22:28:44 |
- +---------------------+----------+---------------------+
可以看到,雖然 中途 sleep 3 秒,但 now() 函數(shù)兩次的時(shí)間值是相同的; sysdate() 函數(shù)兩次得到的時(shí)間值相差 3 秒。MySQL 數(shù)據(jù)庫(kù)Manual 中是這樣描述 sysdate() 的:Return the time at which the function executes。
sysdate() 日期時(shí)間函數(shù),一般情況下很少用到。
2. 獲得當(dāng)前日期(date)函數(shù):curdate()
- mysql> select curdate();
- +------------+
- | curdate() |
- +------------+
- | 2008-08-08 |
- +------------+
其中,下面的兩個(gè)日期函數(shù)等同于 curdate():
current_date()
,current_date3. 獲得當(dāng)前時(shí)間(time)函數(shù):curtime()
- mysql> select curtime();
- +-----------+
- | curtime() |
- +-----------+
- | 22:41:30 |
+-----------+其中,下面的兩個(gè)時(shí)間函數(shù)等同于 curtime():
current_time()
,current_time4. 獲得當(dāng)前 UTC 日期時(shí)間函數(shù):
- utc_date(), utc_time(), utc_timestamp()
- mysql> select utc_timestamp(), utc_date(), utc_time(), now()
- +---------------------+------------+------------+---------------------+
- | utc_timestamp() | utc_date() | utc_time() | now() |
- +---------------------+------------+------------+---------------------+
- | 2008-08-08 14:47:11 | 2008-08-08 | 14:47:11 | 2008-08-08 22:47:11 |
- +---------------------+------------+------------+---------------------+
因 為我國(guó)位于東八時(shí)區(qū),所以本地時(shí)間 = UTC 時(shí)間 + 8 小時(shí)。UTC 時(shí)間在業(yè)務(wù)涉及多個(gè)國(guó)家和地區(qū)的時(shí)候,非常有用。
二、MySQL數(shù)據(jù)庫(kù) 日期時(shí)間 Extract(選?。?函數(shù)。
1. 選取日期時(shí)間的各個(gè)部分:日期、時(shí)間、年、季度、月、日、小時(shí)、分鐘、秒、微秒
- set @dt = '2008-09-10 07:15:30.123456';
- select date(@dt); -- 2008-09-10
- select time(@dt); -- 07:15:30.123456
- select year(@dt); -- 2008
- select quarter(@dt); -- 3
- select month(@dt); -- 9
- select week(@dt); -- 36
- select day(@dt); -- 10
- select hour(@dt); -- 7
- select minute(@dt); -- 15
- select second(@dt); -- 30
- select microsecond(@dt);
1234562. MySQL Extract() 函數(shù),可以上面實(shí)現(xiàn)類似的功能:
- set @dt = '2008-09-10 07:15:30.123456';
- select extract(year from @dt); -- 2008
- select extract(quarter from @dt); -- 3
- select extract(month from @dt); -- 9
- select extract(week from @dt); -- 36
- select extract(day from @dt); -- 10
- select extract(hour from @dt); -- 7
- select extract(minute from @dt); -- 15
- select extract(second from @dt); -- 30
- select extract(microsecond from @dt); -- 123456select extract(year_month from @dt); -- 200809
- select extract(day_hour from @dt); -- 1007
- select extract(day_minute from @dt); -- 100715
- select extract(day_second from @dt); -- 10071530
- select extract(day_microsecond from @dt); -- 10071530123456
- select extract(hour_minute from @dt); -- 715
- select extract(hour_second from @dt); -- 71530
- select extract(hour_microsecond from @dt); -- 71530123456
- select extract(minute_second from @dt); -- 1530
- select extract(minute_microsecond from @dt); -- 1530123456
- select extract(second_microsecond from @dt);
30123456MySQL Extract() 函數(shù)除了沒有date(),time() 的功能外,其他功能一應(yīng)具全。并且還具有選取‘day_microsecond’ 等功能。注意這里不是只選取 day 和 microsecond,而是從日期的 day 部分一直選取到 microsecond 部分。夠強(qiáng)悍的吧!
以上的相關(guān)內(nèi)容就是對(duì)MySQL數(shù)據(jù)庫(kù)的日期函數(shù)、時(shí)間函數(shù)(MySQL 5.X)的部分內(nèi)容介紹,望你能有所收獲。
【編輯推薦】