将纪元时间转换为日期 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13477788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Epoch Time to Date PHP
提问by user1701252
I'm using an API right now and it provides an epochTime. I've tried everything to convert this epochtime to date, but it doesn't seem to be working including $epoch_time / 1000and then using the date()function to convert it.
我现在正在使用 API,它提供了一个 epochTime。我已经尝试了一切将这个时代时间转换为日期,但它似乎没有工作,包括$epoch_time / 1000然后使用该date()函数来转换它。
The epoch time looks something like this 1353430853299. Is there a way to do this? strtotime()did not work either.
纪元时间看起来像这样 1353430853299。有没有办法做到这一点?strtotime()也没有用。
It seems that all of the other readings about epoch time are about changing date to epochtime, but I'm looking to go the other way around. Any help is greatly appreciated.
似乎所有其他关于纪元时间的阅读都是关于将日期更改为纪元时间,但我正在寻找相反的方法。任何帮助是极大的赞赏。
回答by user1701252
Fixed it using substr($epoch, 0, 10) and then used the date function for anyone wondering about the 13 digit epoch times.
使用 substr($epoch, 0, 10) 修复它,然后对任何想知道 13 位纪元时间的人使用日期函数。
Here is a sample code:
这是一个示例代码:
echo date("Y-m-d H:i:s", substr("1477020641000", 0, 10));
// Result: 2016-10-20 20:30:41
回答by Jon
There are a couple different ways you can do this.
有几种不同的方法可以做到这一点。
First off, what you've got there is a Unix Epoch time. (1/1/1970), that makes everything MUCH easier.
首先,您所拥有的是 Unix Epoch 时间。(1/1/1970),这让一切变得更容易。
In PHP, try
在 PHP 中,尝试
$epoch = 1344988800;
$dt = new DateTime("@$epoch");
echo $dt->format('Y-m-d H:i:s');
To display your date
显示您的日期
OR
或者
If you want the long RFC232 date:
如果你想要长的 RFC232 日期:
echo date('r', $epoch);
回答by twodayslate
$epoch = 1344988800;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
More: http://www.epochconverter.com/programming/functions-php.php
更多:http: //www.epochconverter.com/programming/functions-php.php
回答by Rawkode
<?php
$epoch = '1353429562';
date_default_timezone_set('GMT');
echo date('Y-m-d H:i:s', $epoch);
回答by foxontherock
FYI, API that send JSON data use epoch with millisecondes to be compatible with web browsers.
仅供参考,发送 JSON 数据的 API 使用以毫秒为单位的纪元以与 Web 浏览器兼容。
I don't know why PHP needs to remove the millisecondes, as it is the standard for every web browser.
我不知道为什么 PHP 需要删除毫秒,因为它是每个 Web 浏览器的标准。
So, you can use the left 10, or / 1000 (it's better to divide by 1000 if you don't want to get troubles on Nov 20, 2286!!!)
所以,你可以使用左边的10,或者/1000(如果你不想在2286年11月20日遇到麻烦,最好除以1000!!!)
回答by Number1 Dad
echo date("Y-m-d H:i:s", round(1353430853299/1000))works When you divide by 1000 you get 1353430853.299 so you must round it to get rid of the fractional part of the number. You could also use floor if you don't want it rounded.
echo date("Ymd H:i:s", round(1353430853299/1000))有效当您除以 1000 时,您会得到 1353430853.299,因此您必须将其四舍五入以去除数字的小数部分。如果您不希望它四舍五入,您也可以使用地板。

