将 MySQL Unix-Timestamp 格式转换为日期格式?
要实现这一点,以下是语法
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 -> ); Query OK, 0 rows affected (0.59 sec)
使用 insert 命令在表中插入一些记录。查询如下 −
mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1334428200); Query OK, 1 row affected (0.20 sec) mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1513881000); Query OK, 1 row affected (0.15 sec) mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1542738600); Query OK, 1 row affected (0.14 sec) mysql> insert into unixTimeStampFormatDemo(MyTimeStampValue) values(1551637800); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录。查询如下 −
mysql> select *from unixTimeStampFormatDemo;
以下是输出
+----+------------------+ | Id | MyTimeStampValue | +----+------------------+ | 1 | 1334428200 | | 2 | 1513881000 | | 3 | 1542738600 | | 4 | 1551637800 | +----+------------------+ 4 rows in set (0.00 sec)
以下是对 MySQL Unix-Timestamp 格式进行日期时间转换的查询
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)
广告