为此,语法如下
select date_format(from_unixtime(yourColumnName),'%b %d, %Y %l:%i %p PDT') from yourTableName;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table unixTimeStampFormatDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> MyTimeStampValue bigint -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1334428200); mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1513881000); mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1542738600); mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1551637800);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from unixTimeStampFormatDemo;
以下是输出
+----+------------------+ | Id | MyTimeStampValue | +----+------------------+ | 1 | 1334428200 | | 2 | 1513881000 | | 3 | 1542738600 | | 4 | 1551637800 | +----+------------------+ 4 rows in set (0.00 sec)
这是将MySQL Unix-时间戳格式转换为日期时间的查询
mysql> select date_format(from_unixtime(MyTimeStampValue),'%b %d, %Y %l:%i %p PDT') from unixTimeStampFormatDemo;
以下是输出
+-----------------------------------------------------------------------+ | date_format(from_unixtime(MyTimeStampValue),'%b %d, %Y %l:%i %p PDT') | +-----------------------------------------------------------------------+ | Apr 15, 2012 12:00 AM PDT | | Dec 22, 2017 12:00 AM PDT | | Nov 21, 2018 12:00 AM PDT | | Mar 04, 2019 12:00 AM PDT | +-----------------------------------------------------------------------+ 4 rows in set (0.00 sec)