欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL常用日期時(shí)間函數(shù)示例詳解

 更新時(shí)間:2024年06月24日 11:13:10   作者:morris131  
MySQL提供了大量的日期和時(shí)間函數(shù),這些函數(shù)用于在查詢(xún)中處理和操作日期與時(shí)間值,這篇文章主要介紹了MySQL常用日期時(shí)間函數(shù),需要的朋友可以參考下

MySQL提供了大量的日期和時(shí)間函數(shù),這些函數(shù)用于在查詢(xún)中處理和操作日期與時(shí)間值。

參考官方文檔:https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html

獲取日期時(shí)間函數(shù)

now():獲取當(dāng)前的日期和時(shí)間,包含年、月、日、時(shí)、分、秒。

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-06-18 07:35:45 |
+---------------------+
1 row in set (0.00 sec)

curdate():獲取當(dāng)前的日期,只包含年、月、日部分。

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2024-06-18 |
+------------+
1 row in set (0.00 sec)

curtime():獲取當(dāng)前的時(shí)間,只包含時(shí)、分、秒部分。

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 07:37:31  |
+-----------+
1 row in set (0.00 sec)

日期與時(shí)間戳的轉(zhuǎn)化

unix_timestamp:將日期轉(zhuǎn)換為UNIX時(shí)間戳。

mysql> select unix_timestamp(now());
+-----------------------+
| unix_timestamp(now()) |
+-----------------------+
|            1718696453 |
+-----------------------+
1 row in set (0.00 sec)

from_unixtime(timestamp)函數(shù):是將UNIX時(shí)間戳轉(zhuǎn)化為日期時(shí)間,與unix_timestamp(date)函數(shù)互為反函數(shù)。

mysql> select from_unixtime(1718696453);
+---------------------------+
| from_unixtime(1718696453) |
+---------------------------+
| 2024-06-18 07:40:53       |
+---------------------------+
1 row in set (0.00 sec)

日期格式化

date_format():將日期轉(zhuǎn)化為指定格式的字符串。

mysql> select date_format(now(), '%Y-%m-%d %H:%i:%s');
+-----------------------------------------+
| date_format(now(), '%Y-%m-%d %H:%i:%s') |
+-----------------------------------------+
| 2024-06-18 07:47:16                     |
+-----------------------------------------+
1 row in set (0.00 sec)

str_to_date():將指定格式的字符串轉(zhuǎn)化為日期。

mysql> select str_to_date('2024-06-18 07:47:16', '%Y-%m-%d %H:%i:%s');
+---------------------------------------------------------+
| str_to_date('2024-06-18 07:47:16', '%Y-%m-%d %H:%i:%s') |
+---------------------------------------------------------+
| 2024-06-18 07:47:16                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

下面是一些常用的日期格式符號(hào):

  • %Y:四位數(shù)的年份(如:2022)
  • %y:兩位數(shù)的年份(如:22)
  • %m:兩位數(shù)的月份(01~12)
  • %d:兩位數(shù)的日期(01~31)
  • %H:兩位數(shù)的小時(shí)(00~23)
  • %i:兩位數(shù)的分鐘(00~59)
  • %s:兩位數(shù)的秒鐘(00~59)

更多的日期格式符號(hào)可以參考官方文檔。

注意:在使用str_to_date函數(shù)時(shí),請(qǐng)確保傳入的日期字符串和格式字符串的格式匹配,否則可能會(huì)得到錯(cuò)誤的結(jié)果。

日期時(shí)間的運(yùn)算

date_add(date,INTERVAL expr unit):在指定日期上增加時(shí)間。

date_sub(date,INTERVAL expr unit),在指定日期上減去時(shí)間。

參數(shù)說(shuō)明:

  • date參數(shù)指定開(kāi)始日期或日期時(shí)間值。
  • expr是一個(gè)表達(dá)式,指定從開(kāi)始日期加上或減去的間隔值;expr被計(jì)算為一個(gè)字符串;它可以以-開(kāi)頭表示負(fù)間隔。
  • unit是一個(gè)關(guān)鍵字,指示表達(dá)式應(yīng)使用的單位。
mysql> select date_add('2024-04-10',INTERVAL 1 DAY);
+---------------------------------------+
| date_add('2024-04-10',INTERVAL 1 DAY) |
+---------------------------------------+
| 2024-04-11                            |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('2024-04-10',INTERVAL -1 DAY);
+----------------------------------------+
| date_add('2024-04-10',INTERVAL -1 DAY) |
+----------------------------------------+
| 2024-04-09                             |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2024-04-10',INTERVAL -1 DAY);
+----------------------------------------+
| date_sub('2024-04-10',INTERVAL -1 DAY) |
+----------------------------------------+
| 2024-04-11                             |
+----------------------------------------+
1 row in set (0.00 sec)
mysql> select date_sub('2024-04-10',INTERVAL 1 DAY);
+---------------------------------------+
| date_sub('2024-04-10',INTERVAL 1 DAY) |
+---------------------------------------+
| 2024-04-09                            |
+---------------------------------------+
1 row in set (0.00 sec)

datediff(expr1,expr2)函數(shù):返回expr1−expr2,以從一個(gè)日期到另一個(gè)日期的天數(shù)表示。其中expr1和expr2是日期或日期和時(shí)間表達(dá)式;計(jì)算中只使用值的日期部分。

mysql> select datediff('2024-04-10', '2024-05-10');
+--------------------------------------+
| datediff('2024-04-10', '2024-05-10') |
+--------------------------------------+
|                                  -30 |
+--------------------------------------+
1 row in set (0.00 sec)

到此這篇關(guān)于MySQL常用日期時(shí)間函數(shù)的文章就介紹到這了,更多相關(guān)MySQL常用日期時(shí)間函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論