Convert UNIX timestamp into human readable format in MySQL?

MySQLMySQLi Database

To convert UNIX timestamp into a human-readable format, use the FROM_UNIXTIME() method.

Let us first create a table −

mysql> create table timeConversionDemo
   -> (
   -> dateTimeConversion bigint
   -> );
Query OK, 0 rows affected (0.45 sec)

Following is the query to insert records in the table using insert command −

mysql> insert into timeConversionDemo values(1554316200);
Query OK, 1 row affected (0.14 sec)

mysql> insert into timeConversionDemo values(1546194600);
Query OK, 1 row affected (0.22 sec)

mysql> insert into timeConversionDemo values(1511548200 );
Query OK, 1 row affected (0.21 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from timeConversionDemo;

This will produce the following output −

+--------------------+
| dateTimeConversion |
+--------------------+
| 1554316200         |
| 1546194600         |
| 1511548200         |
+--------------------+
3 rows in set (0.00 sec)

Here is the query to convert −

mysql> select FROM_UNIXTIME(dateTimeConversion,'%d-%m-%Y') AS Conversion from
timeConversionDemo;

This will produce the following output −

+------------+
| Conversion |
+------------+
| 04-04-2019 |
| 31-12-2018 |
| 25-11-2017 |
+------------+
3 rows in set (0.00 sec)

Following is the query if you want it in the MySQL date format −

mysql> select FROM_UNIXTIME(dateTimeConversion,'%Y-%m-%d') AS Conversion from
timeConversionDemo;

This will produce the following output −

+------------+
| Conversion |
+------------+
| 2019-04-04 |
| 2018-12-31 |
| 2017-11-25 |
+------------+
3 rows in set (0.00 sec)
raja
Published on 16-Apr-2019 16:55:30
Advertisements