Convert MM/DD/YY to Unix timestamp in MySQL?


To convert MM/DD/YY to UNIX timestamp, you can use the below syntax −

select UNIX_TIMESTAMP(str_to_date(yourColumnName,'%m/%d/%Y')) from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   dateConvertToUnix varchar(100)
   );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(dateConvertToUnix) values('01/10/2001');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(dateConvertToUnix) values('03/31/2010');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(dateConvertToUnix) values('12/31/2016');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(dateConvertToUnix) values('04/27/2019');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+----+-------------------+
| Id | dateConvertToUnix |
+----+-------------------+
| 1  | 01/10/2001        |
| 2  | 03/31/2010        |
| 3  | 12/31/2016        |
| 4  | 04/27/2019        |
+----+-------------------+
4 rows in set (0.00 sec)

Following is the query to convert MM/DD/YY to UNIX timestamp −

mysql> select UNIX_TIMESTAMP(str_to_date(dateConvertToUnix,'%m/%d/%Y')) from DemoTable;

This will produce the following output −

+-----------------------------------------------------------+
| UNIX_TIMESTAMP(str_to_date(dateConvertToUnix,'%m/%d/%Y')) |
+-----------------------------------------------------------+
| 979065000                                                 |
| 1269973800                                                |
| 1483122600                                                |
| 1556303400                                                |
+-----------------------------------------------------------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements