将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)
阅读更多:MySQL 教程