简体   繁体   English

PHP 和 mysql 中的 Epoch 和 Unix 时间戳转换无法正常工作

[英]Epoch & Unix Timestamp Conversion in PHP and mysql is not working properly

1473424835535 1473424835535

http://www.epochconverter.com/ http://www.epochconverter.com/

using this which gives exact date使用这个给出确切日期

Result : GMT: Fri, 09 Sep 2016 12:40:35.535 GMT结果:格林威治标准时间:2016 年 9 月 9 日星期五 12:40:35.535 GMT

but in PHP但在 PHP 中

$returnValue = date('d.m.Y H:i:s', 1473424835535);

Result : 22.12.48660 05:32:15

tried in mysql also也在mysql中试过

select FROM_UNIXTIME('1473424835535', '%Y %D %M %h:%i:%s %x') 

not working不工作

Note : This record is migrated from google api注意:此记录是从 google api 迁移的

How to rectify this issue or any method to rectify this issue?如何纠正此问题或纠正此问题的任何方法? Any help will be appreciated.任何帮助将不胜感激。

Your timestamp seems to be in milliseconds , while both php's and MySQL timestamp implementations work on a seconds basis.您的时间戳似乎以毫秒为单位,而 php 和 MySQL 时间戳实现都以单位 Solution: divide your timestamp by 1000:解决方案:将时间戳除以 1000:

FROM_UNIXTIME(1473424835535/1000,'%Y-%M-%d %H:%i:%s %f') 

As stated on the very site you linked to:正如您链接到的网站上所述:

Assuming that this timestamp is in milliseconds假设这个时间戳以毫秒为单位

The PHP and MySQL manual states that the timestamp they take is "seconds since Unix epoch". PHP 和 MySQL 手册指出,它们采用的时间戳是“自 Unix 纪元以来的秒数”。

A quick way to determine whether or not you have a valid timestamp, as defined by the manual, is to count the number of digits.确定您是否具有手册中定义的有效时间戳的一种快速方法是计算位数。 Abovementioned site has a common timestamp listed as well, which you can use for comparison.上面提到的站点也列出了一个通用的时间戳,您可以用来进行比较。

FROM_UNIXTIME(1473424835535/1000,'%Y-%M-%d %H:%i:%s %f') is the way os selecting from Mysql and FROM_UNIXTIME(1473424835535/1000,'%Y-%M-%d %H:%i:%s %f') 是os从Mysql中选择的方式,

Proper PHP Way is echo date('dmY H:i:s', (int) 1473424835535/1000);正确的 PHP 方式是 echo date('dmY H:i:s', (int) 1473424835535/1000);

Thanks for Mark Baker and Shadow谢马克贝克影子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM