Convert MySQL Unix-Timestamp format to date format?


To achieve this, the following is the syntax

select date_format(from_unixtime(yourColumnName),'%b %d, %Y %l:%i %p PDT') from yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table unixTimeStampFormatDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> MyTimeStampValue bigint
   -> );
Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command. The query is as follows −

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)

Display all records from the table using select statement. The query is as follows −

mysql> select *from unixTimeStampFormatDemo;

The following is the output

+----+------------------+
| Id | MyTimeStampValue |
+----+------------------+
|  1 | 1334428200       |
|  2 | 1513881000       |
|  3 | 1542738600       |
|  4 | 1551637800       |
+----+------------------+
4 rows in set (0.00 sec)

Here is the query to convert MySQL Unix-Timestamp Format to datetime

mysql> select date_format(from_unixtime(MyTimeStampValue),'%b %d, %Y %l:%i %p PDT') from unixTimeStampFormatDemo;

The following is the output

+-----------------------------------------------------------------------+
| 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)

Updated on: 30-Jul-2019

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements