- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements