首頁  >  文章  >  資料庫  >  mysql日期轉換函數有哪些

mysql日期轉換函數有哪些

青灯夜游
青灯夜游原創
2021-12-02 15:20:4621808瀏覽

mysql日期轉換函數有:1、date_format();2、time_format();3、str_to_date();4、to_days();5、from_days();6、time_to_sec();7、 sec_to_time()等。

mysql日期轉換函數有哪些

本教學操作環境:windows7系統、mysql8版本、Dell G3電腦。

MySQL 日期轉換函數、時間轉換函數

1,MySQL Date/Time to Str(日期/時間轉換為字串)函數:date_format( date,format), time_format(time,format)

函數:date_format('2008-08-08 22:23:01', '%Y %m%d%H%i%s')

結果:20080808222301

MySQL 日期、時間轉換函數:date_format (date,format), time_format(time,format) 能夠把一個日期/時間轉換成各種各樣的字串格式。它是 str_to_date(str,format) 函數的 一個逆轉換。

2,MySQL Str to Date (字串轉換為日期)函數:str_to_date(str, format)

select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30

str_to_date(str,format) 轉換函數,可以把一些雜亂無章的字串轉換成日期格式。

3,MySQL (日期、天數)轉換函數:to_days(date), from_days(days)

select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627

4,MySQL (時間、秒)轉換函數:time_to_sec(time), sec_to_time(seconds)

select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'

5,MySQL 拼湊日期、時間函數:makdedate( year,dayofyear), maketime(hour,minute,second)

select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'

6,MySQL (Unix 時間戳記、日期)轉換函數:

unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)

【相關推薦:mysql影片教學

以上是mysql日期轉換函數有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn