MYSQL時間戳和日期相互轉換 筆記整理

2022-08-16 22:24:13 字數 1930 閱讀 9323

date_format(date, format) 函式,mysql日期格式化函式date_format()

unix_timestamp() 函式

str_to_date(str, format) 函式

from_unixtime(unix_timestamp, format) 函式,mysql時間戳格式化函式from_unixtime

日期格式化:

select date_format(now(), '%y-%m-%d');  

輸出:2018-08-03

日期轉時間戳:

select unix_timestamp(now()); 

輸出:1533262442

字串轉日期:

select str_to_date('09/20/2018','%m/%d/%y');

輸出:2018-09-20

select str_to_date('20180802154706','%y%m%d%h%i%s');

輸出:2018-08-02 15:47:06

select str_to_date('2018-08-02 15:47:06','%y-%m-%d %h:%i:%s');

輸出:2018-08-02 15:47:06

日期轉時間戳:

select unix_timestamp('2018-08-02');

輸出:1533139200

select from_unixtime(1533149211);

輸出:2018-08-02 02:46:51

select from_unixtime(1533149211,'%y-%m-%d'); 

輸出:2018-08-02

mysql日期格式化(format)取值範圍。值含義

秒%s、%s

兩位數字形式的秒( 00,01, ..., 59)

分%i、%i

兩位數字形式的分( 00,01, ..., 59)

小時 %h

24小時制,兩位數形式小時(00,01, ...,23)

%h12小時制,兩位數形式小時(00,01, ...,12)

%k24小時制,數形式小時(0,1, ...,23)

%l12小時制,數形式小時(0,1, ...,12)

%t24小時制,時間形式(hh:mm:ss)

%r12小時制,時間形式(hh:mm:ss am 或 pm)

%p am上午或pm下午 

周 %w

一周中每一天的名稱(sunday,monday, ...,saturday)

%a一周中每一天名稱的縮寫(sun,mon, ...,sat) 

%w 以數字形式標識周(0=sunday,1=monday, ...,6=saturday) 

%u數字表示週數,星期天為週中第一天

%u數字表示週數,星期一為週中第一天

天%d 

兩位數字表示月中天數(01,02, ...,31)

%e 數字表示月中天數(1,2, ...,31)

%d英文本尾表示月中天數(1st,2nd,3rd ...) 

%j以三位數字表示年中天數(001,002, ...,366) 

月%m 

英文月名(january,february, ...,december) 

%b 英文縮寫月名(jan,feb, ...,dec) 

%m 兩位數字表示月份(01,02, ...,12)

%c 數字表示月份(1,2, ...,12) 

年%y 

四位數字表示的年份(2015,2016...)

%y 兩位數字表示的年份(15,16...)

文字輸出 

%文字 

直接輸出文字內容

時間戳 日期相互轉換

當天0點日期 date y m d h i s mktime 0,0,0,date m date d date y 當天0點時間戳 mktime 0,0,0,date m date d date y 當前日期 time date y m d h i s 當前時間戳 unixtime strtotim...

mysql 中時間戳和日期相互轉換

mysql將時間戳直接轉換成日期時間 from unixtime 是mysql裡的時間函式 select uid,userid,username,email,from unixtime time,y年 m月 d from members 以上例子是sql語句的例子,直接能將mysql的時間戳型別 t...

jquery時間戳和日期相互轉換

網上找的很多都沒都是這樣顯示的2017 8 7 3 5 3 自己搜尋改下了一下加了0這樣顯示 2017 08 07 15 05 03 function 日期 轉換為 unix時間戳 param 2014 01 01 20 20 20 日期格式 return unix時間戳 秒 datetounix ...