目錄
1. 獲取當前時間和日期
2. 獲取當前日期
3. 獲取當前時間
4. 獲取單獨的年/月/日/時/分/秒?
5. 添加時間間隔 date_add ( )
6. 格式化日期 date_format?( )
7. 字符串轉日期 str_to_date ()
8. 第幾天 dayofxx
9. 當月最后一天 last_day ( )
10. 日期差 datediff ( )?
11. 時間差 timediff ( )
1. 獲取當前時間和日期
now();
sysdata ( ) ;
?
區別:
select now ( ) ; 輸出的時間點是開始執行 select 關鍵詞的時間;
select sysdate();輸出的的時間點是開始執行 sysdate 函數的時間點;
2. 獲取當前日期
curdata();
current_date();
3. 獲取當前時間
?curtime ( ) ;??
current_time;
?current_time ( ) ;
4. 獲取單獨的年/月/日/時/分/秒?
獲取單獨的年:year ( now ( ) ) ;
獲取單獨的月:month ( now ( ) ) ;
獲取單獨的日:day ( now ( ) ) ;
獲取單獨的時:hour?( now ( ) ) ;
獲取單獨的分:minute?( now ( ) ) ;
獲取單獨的秒:second ( now ( ) ) ;
5. 添加時間間隔 date_add ( )
增加時間:date_add ( ' 時間 ' , interval? 數字 單位)?;
減少時間:date_sub ( ' 時間 ' , interval? 數字 單位) 或者?date_add ( ' 時間 ' , interval? 負數?單位)?;
另外單位還有 quarter(季度),week(周)等;
還有復合單位;year_month(幾年幾月之后) , month_week (幾月幾周之后)等;
6. 格式化日期 date_format?( )
一般用于查詢操作;?
SQL語句:date_format (日期,格式?),將時間按照指定格式輸出?;
?
?另外,在使用 now()函數獲取當前日期和時間時,底層也調用了date_format(),只不過這是一種隱式轉換,而且隱式轉換的前提是日期必須為 date 標準格式;
7. 字符串轉日期 str_to_date ()
一般用于插入操作;?
在后面學習插入數據時,插入date類型的數據可以
(1)使用 str_date()將 ‘ %h /%i/ %d/ ’ 字符串格式的日期轉換為 ‘ %h -%i-?%d/?’ 字符串格式的日期,再默認轉換為date格式的日期;
(2)將 ‘ %h -%i-?%d/?’ 字符串格式的日期,默認轉換為date格式的日期;
?隱式轉換建立在MySQL標準格式(%h -%i-?%d/)上;
8. 第幾天 dayofxx
一年中的第幾天:dayofyear ( )
一月中的第幾天:dayofmonth ( )
一周中的第幾天:dayofweek?( )
9. 當月最后一天 last_day ( )
?
10. 日期差 datediff ( )?
只與日期有關,與時間無關;?
11. 時間差 timediff ( )
時間差與日期有關,因為是計算的結果是以 ‘ -時-分-秒 ’ 的格式輸出;?