MySql UNIX_TIMESTAMP和FROM_UNIXTIME函數講解
by:授客 QQ:1033553122
1. unix_timestamp(date)將時間轉換為時間戳,如果參數為空,則處理的是當前的時間(返回從'1970-01-01 00:00:00'GMT開始的到當前時間的秒數,不為空則它返回從'1970-01-01 00:00:00' GMT開始的到指定date的秒數值),date可以是一個DATE字符串、一個DATETIME字符串、一個TIMESTAMP或以YYMMDD或YYYYMMDD格式的本地時間的一個數字。
select unix_timestamp();
結果:
2 from_unixtime將時間戳轉換為時間,返回表示 Unix 時間標記的一個字符串,根據format字符串格式化。format可以包含與DATE_FORMAT()函數列出的條目同樣的修飾符。
select from_unixtime(1348737229);
結果:
3.select unix_timestamp('2012-09-27');
結果:
4. select unix_timestamp('2012-09-27 17:13:49');
結果:
5. select from_unixtime('2012-09-27 17:13:49');
結果:
6. select from_unixtime('2011-09-25 17:13:49');
結果:
7. select now();
結果-顯示系統當前時間
select unix_timestamp(now());
結果:
8.假如你向unix_timestamp()傳遞一個溢出日期,它會返回NULL,但請注意只有基本范圍檢查會被執行 (年份從1970 到 2037, 月份從01 到12,日期從 01 到31)。
select unix_timestamp('1969-09-25 17:13:49');
結果
select unix_timestamp('2038-09-25 17:13:49');
結果
9 select from_unixtime(1348738577, '%Y%m%d');
結果:
select from_unixtime(1348738577, '%y%m%d');
結果:
select from_unixtime(1348738577, '%Y年%m月%d日');
結果:
注:
根據format字符串格式化date值。
下列修飾符可以被用在format字符串中:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%w 一個星期中的天數(0=Sunday ……6=Saturday )
%U 星期(0……52), 這里星期天是星期的第一天
%u 星期(0……52), 這里星期一是星期的第一天
%D 有英語前綴的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 數字, 4 位
%y 年, 數字, 2 位
%a 縮寫的星期名字(Sun……Sat)
%d 月份中的天數, 數字(00……31)
%e 月份中的天數, 數字(0……31)
%m 月, 數字(01……12)
%c 月, 數字(1……12)
%b 縮寫的月份名字(Jan……Dec)
%j 一年中的天數(001……366)
%H 小時(00……23)
%k 小時(0……23)
%h 小時(01……12)
%I 小時(01……12)
%l 小時(1……12)
%i 分鍾, 數字(00……59)
%r 時間,12 小時(hh:mm:ss [AP]M)
%T 時間,24 小時(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%% 一個文字“%”。