MySQL:日期,字符串,時間戳之間的互相轉換

MySQL:日期,字符串,時間戳之間的互相轉換

一、數據的獲取
1.獲取當前時間戳

select unix_timestamp();

2.獲取當前時間

select now();

二、數據的轉換
1.《時間戳》轉《時間》使用from_unixtime函數

select from_unixtime(1515980716);

2.《時間戳》轉《字符串》使用from_unixtime函數

SELECT from_unixtime(1515980716, '%Y-%m-%d %H:%i:%S');

3.《時間》轉《字符串》使用date_format函數

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

4.《時間》轉《時間戳》使用unix_timestamp函數

select unix_timestamp(now());

5.《字符串》轉《時間戳》使用unix_timestamp函數

select unix_timestamp('2018-12-26 15:45:10');

6.《字符串》轉《時間》使用str_to_date函數

select str_to_date('2018-12-26', '%Y-%m-%d');  

三、SQL示例
1.查詢當天的數據

select * from table_request where date_format(from_unixtime(時間戳),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');

四、MySQL日期格式化(format)取值範圍

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章