MySQL时间戳格式化函数from_unixtime()

/ 1评 / 28

语法

mysql中,格式化时间戳函数为from_unixtime

函数:from_unixtime( unix_timestamp,format)

参数:unix_timestamp:时间戳、format:日期格式

举个栗子

mysql > select from_unixtime(1451997924,'%Y')

20160105204625

含义
%S、%s 两位数字形式的秒( 00,01, ..., 59)
%I、%i 两位数字形式的分( 00,01, ..., 59)
小时 %H 24小时制,两位数形式小时(00,01, ...,23)
%h 12小时制,两位数形式小时(00,01, ...,12)
%k 24小时制,数形式小时(0,1, ...,23)
%l 12小时制,数形式小时(0,1, ...,12)
%T 24小时制,时间形式(HH:mm:ss)
%r 12小时制,时间形式(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...)
文字输出 %文字 直接输出文字内容

常用格式

格式1:yyyy-MM-dd

select from_unixtime(1451997924,'%Y%-%c%-%e');

格式2:HH:mm:ss

select from_unixtime(1451997924,'%k%:%i%:%s');
-- 或 --
select from_unixtime(1451997924,'%T');

格式3:yyyy年MM月dd日 HH:mm:ss

select from_unixtime(1451997924,'%Y%年%c%月%e%日 %k%:%i%:%s');
-- 或 --
select from_unixtime(1451997924,'%Y%年%c%月%e%日 %T');
  1. […] from_unixtime(unix_timestamp, format) 函数,MySQL时间戳格式化函数from_unixtime() […]

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注