在 MySQL 中将 MM/DD/YY 转换为 Unix 时间戳?
要将 MM/DD/YY 转换为 UNIX 时间戳,可以使用以下语法 −
select UNIX_TIMESTAMP(str_to_date(yourColumnName,'%m/%d/%Y')) from yourTableName;
让我们首先创建一个表 −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, dateConvertToUnix varchar(100) ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入一些记录 −
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)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable;
将会产生以下输出 −
+----+-------------------+ | 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)
以下是将 MM/DD/YY 转换为 UNIX 时间戳的查询 −
mysql> select UNIX_TIMESTAMP(str_to_date(dateConvertToUnix,'%m/%d/%Y')) from DemoTable;
将会产生以下输出 −
+-----------------------------------------------------------+ | UNIX_TIMESTAMP(str_to_date(dateConvertToUnix,'%m/%d/%Y')) | +-----------------------------------------------------------+ | 979065000 | | 1269973800 | | 1483122600 | | 1556303400 | +-----------------------------------------------------------+ 4 rows in set (0.00 sec)
广告