Convert MySQL timestamp to UNIX Timestamp?

MySQLMySQLi Database

To convert MySQL timestamp to UNIX Timestamp, use the UNIX_TIMESTAMP(). Following is the syntax −

select unix_timestamp(yourColumnName) from yourTableName;

Let us first create a table −

mysql> create table DemoTable(
   Duetimestamp timestamp
);
Query OK, 0 rows affected (2.66 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(now());
Query OK, 1 row affected (1.53 sec)
mysql> insert into DemoTable values('2016-01-21 12:34:00');
Query OK, 1 row affected (0.73 sec)
mysql> insert into DemoTable values('2018-05-01 02:00:00');
Query OK, 1 row affected (0.42 sec)
mysql> insert into DemoTable values('2017-03-02 01:10:20');
Query OK, 1 row affected (10.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------+
| Duetimestamp        |
+---------------------+
| 2019-08-20 20:48:45 |
| 2016-01-21 12:34:00 |
| 2018-05-01 02:00:00 |
| 2017-03-02 01:10:20 |
+---------------------+
4 rows in set (0.00 sec)

Following is the query to convert MySQL timestamp to UNIX Timestamp −

mysql> select unix_timestamp(Duetimestamp) from DemoTable;

This will produce the following output −

+------------------------------+
| unix_timestamp(Duetimestamp) |
+------------------------------+
| 1566314325                   |
| 1453359840                   |
| 1525120200                   |
| 1488397220                   |
+------------------------------+
4 rows in set (0.04 sec)
raja
Published on 27-Sep-2019 11:28:17
Advertisements