mysql日期格式轉(zhuǎn)換函數(shù)有:1、【sec_to_time(seconds)】把秒數(shù)轉(zhuǎn)化成時(shí)間;2、【time_to_sec(time)】把時(shí)間轉(zhuǎn)化成秒數(shù);3、【addtime(time,times)】把times加到time上。
【相關(guān)學(xué)習(xí)推薦:mysql教程(視頻)】
mysql日期格式轉(zhuǎn)換函數(shù)有:
1、獲取系統(tǒng)當(dāng)前日期
select curdate() select current_date()-> 2016-01-16-> 2016-01-162、獲取系統(tǒng)當(dāng)前時(shí)間
select curtime() select current_time()-> 17:44:22-> 17:44:223、now(),sysdate(),current_timestamp(),localtime():獲取系統(tǒng)當(dāng)前日期和時(shí)間
select now() select sysdate() select current_timestamp() select current_timestamp select localtime() select localtime-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:41-> 2016-01-16 17:44:414、unix_timestamp(date):獲取時(shí)間戳
select unix_timestamp() select unix_timestamp(‘2016-01-16') select unix_timestamp(‘2016-01-16 23:59:59')-> 1452937627-> 1452873600-> 14529599995、from_unixtime(unix_timestamp,format):把時(shí)間戳轉(zhuǎn)化成日期時(shí)間
select from_unixtime(1452959999) select from_unixtime(1452959999,'%y-%m-%d %h:%i:%s')-> 2016-01-16 23:59:59-> 2016-01-16 23:59:596、sec_to_time(seconds):把秒數(shù)轉(zhuǎn)化成時(shí)間
select sec_to_time(2378)-> 00:39:387、time_to_sec(time):把時(shí)間轉(zhuǎn)化成秒數(shù)
select time_to_sec(‘22:23:00')-> 23788、addtime(time,times):把times加到time上
select addtime(“2015-12-31 23:59:59”,'01:01:01')-> 2016-01-01 01:01:009、convert_tz(date,from_tz ,to_tz ):轉(zhuǎn)換時(shí)區(qū)
select convert_tz(‘2004-01-01 12:00:00',' 00:00',' 10:00')-> 2004-01-01 22:00:0010、str_to_date(date,format ):將字符串轉(zhuǎn)成format格式的日期時(shí)間
select str_to_date(‘2015-01-01', ‘%y-%m-%d')-> 2015-01-0111、last_day(date ):獲取date當(dāng)月最后一天的日期
select last_day(sysdate()) select last_day(‘2015-02-02') select last_day(‘2015-02-02 00:22:33')-> 2016-01-31-> 2015-02-28-> 2015-02-2812、makedate(year ,dayofyear ):根據(jù)參數(shù)(年份,第多少天)獲取日期
select makedate(2015 ,32)-> 2015-02-0113、 maketime(hour ,minute ,second ):根據(jù)參數(shù)(時(shí),分,秒)獲取時(shí)間
select maketime(12 ,23 ,34 )-> 12:23:3414、yearweek(date):獲取日期的年和周
select yearweek(sysdate()) select yearweek(‘2015-01-10') select yearweek(‘2015-01-10',1)-> 201602-> 201501-> 20150215、weekofyear(date):獲取當(dāng)日是當(dāng)年的第幾周
select weekofyear(sysdate()) select weekofyear(‘2015-01-10')-> 2-> 2mysql中常用的幾種時(shí)間格式轉(zhuǎn)換函數(shù)整理如下
1,from_unixtime(timestamp, format):
timestamp為int型時(shí)間,如14290450779;format為轉(zhuǎn)換的格式,包含格式如下:
%m 月名字(january……december)
%w 星期名字(sunday……saturday)
%d 有英語前綴的月份的日期(1st, 2nd, 3rd, 等等。)
%y 年, 數(shù)字, 4 位
%y 年, 數(shù)字, 2 位
%a 縮寫的星期名字(sun……sat)
%d 月份中的天數(shù), 數(shù)字(00……31)
%e 月份中的天數(shù), 數(shù)字(0……31)
%m 月, 數(shù)字(01……12)
%c 月, 數(shù)字(1……12)
%b 縮寫的月份名字(jan……dec)
%j 一年中的天數(shù)(001……366)
%h 小時(shí)(00……23)
%k 小時(shí)(0……23)
%h 小時(shí)(01……12)
%i 小時(shí)(01……12)
%l 小時(shí)(1……12)
%i 分鐘, 數(shù)字(00……59)
%r 時(shí)間,12 小時(shí)(hh:mm:ss [ap]m)
%t 時(shí)間,24 小時(shí)(hh:mm:ss)
%s 秒(00……59)
%s 秒(00……59)
%p am或pm
%w 一個(gè)星期中的天數(shù)(0=sunday ……6=saturday )
%u 星期(0……52), 這里星期天是星期的第一天
61f期(0……52), 這里星期一是星期的第一
2,unix_timestamp(date):
作用與from_unixtime()剛好相反,前者是把unix時(shí)間戳轉(zhuǎn)換為可讀的時(shí)間,而unix_timestamp()是把可讀的時(shí)間轉(zhuǎn)換為unix時(shí)間戳,這在對datetime存儲(chǔ)的時(shí)間進(jìn)行排序時(shí)會(huì)用到。如unix_timestamp('2009-08-06 10:10:40'),得到1249524739。
如果unix_timestamp()不傳參數(shù),則調(diào)用now()函數(shù)自動(dòng)取當(dāng)前時(shí)間。
3,date_format(date, format):
date_format()是將date或datetime類型值轉(zhuǎn)換為任意的時(shí)間格式。比如常見的應(yīng)用場景,某表有一個(gè)字段是更新時(shí)間,存儲(chǔ)的是datetime類型,但前臺展示時(shí)只需要顯示年月日(xxxx-xx-xx),這個(gè)時(shí)候就可以用date_format(date,'%y-%m-%d ')處理,而不需要在結(jié)果集中用程序循環(huán)處理。
想了解更多編程學(xué)習(xí),敬請關(guān)注php培訓(xùn)欄目!