我正在尝试使用TIMESTAMP
字段将DATETIME
转换为DATE
,TIME
和FROM_UNIXTIME
MySQL类型。我注意到TIME
和DATETIME
字段的值有不同的时间信息。
而不是对表的insert语句,我用一个打印值的简单select语句替换它:
select FROM_UNIXTIME('1468561341') as timestamp_datetime,FROM_UNIXTIME('1468561341','%d/%m/%y') as timestamp_date, FROM_UNIXTIME('1468561341','%h:%m:%s %p') as timestamp_time
结果如下:
2016-07-15 05:42:21.000000
15/07/16
05:07:21 AM
如何在这两列中获得一致的时间值?
答案 0 :(得分:1)
问题在于字母m
应该是我
mysql> select FROM_UNIXTIME(1468561341) as timestamp_datetime,FROM_UNIXTIME(1468561341,'%d/%m/%y') as timestamp_date, FROM_UNIXTIME(1468561341,'%h:%i:%s %p') as timestamp_time;
+---------------------+----------------+----------------+
| timestamp_datetime | timestamp_date | timestamp_time |
+---------------------+----------------+----------------+
| 2016-07-15 05:42:21 | 15/07/16 | 05:42:21 AM |
+---------------------+----------------+----------------+
1 row in set (0.05 sec)
答案 1 :(得分:0)
你的问题是你的FROM_UNIXTIME实际上是在告诉它
hours-months-seconds -AM/PM
但你想要
hours-minutes-seconds -AM/PM
所以将其改为
FROM_UNIXTIME(1468561341,'%h:%i:%s %p')
代替。
MySQL中所有时间参数和时间函数用法的参考指南:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html