DateTime->格式(epoch)返回错误的日期 [英] DateTime->format(epoch) returning the wrong date

查看:124
本文介绍了DateTime->格式(epoch)返回错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,我将一个问题的格式化为一个时代,让人难以理解。

I am working on a project and I am having an issue formatting an epoch time to a human readable time.

我有以下时期 1428512160 ,当我把这个通过epochconverter.com我得到人的时间 08/04/2015 17:56:00 GMT + 1:00 DST 如预期。

I have the following epoch time 1428512160 and when I put this through epochconverter.com I get the human time of 08/04/2015 17:56:00 GMT+1:00 DST as expected.

然后我使用以下代码执行从时代到人类日期的转换。

I then use the following code in order to perform the conversion from the epoch time to a human date time.

$dt = new DateTime($supportDetails["Reported"]);
$reportedTimeString = $dt->format('d-m-Y H:i:s');

$ supportDetails [已报告]是时代(我打印出来,所以我知道它的正确) 。

$supportDetails[Reported] is the epoch time (I've printed it so I know its correct).

然而,我收到的结果是 08-04-2160 14:28:51

The result I get back however is 08-04-2160 14:28:51.

感谢您的帮助。

推荐答案

您需要在DateTime中为时间戳添加 @ 类似这样:

You need to add an @ for the timestamp in the DateTime class, like this:

$dt = new DateTime("@" . $supportDetails["Reported"]);
                  //^ See here

您还可以在手动。并从那里引用:

You can also see this in the manual. And a quote from there:


Unix时间戳 @ ? [0-9] +@ 1215282385

Unix Timestamp "@" "-"? [0-9]+ "@1215282385"

另请注意,当前时区被忽略,您还可以在手册

Also note that the current timezone is getting ignored, which you can also see in the manual:


注意:
$ time $ timezone参数和当前时区被忽略 >参数是 UNIX时间戳(例如 @ 946684800 )或指定时区(例如,2010-01-28T15:00:00 + 02:00)。

Note: The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

这篇关于DateTime->格式(epoch)返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆