php 尝试从秒转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12291195/
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
Trying to convert from seconds to date
提问by Kynian
Okay so I have an array of results from a table, we have a start and end time we are retrieving. the start/end time would look something like this:
好的,所以我有一个表中的结果数组,我们有一个开始和结束时间我们正在检索。开始/结束时间如下所示:
1345497551
Now I'm trying to convert this to a real time, for instance 1345497551might become 2012/05/09 17:23:12or something. I've found a few things but none seem to work correctly. one solution I tried, according to what someone was saying on another question on here, was
现在我正在尝试将其转换为实时,例如1345497551可能会变成2012/05/09 17:23:12什么。我发现了一些东西,但似乎没有一个能正常工作。我尝试过的一个解决方案,根据有人在这里的另一个问题上所说的,是
$createdate = date('H:i:s',$numberofsecs);
where $numberofsecswas the time pulled in from the array. but this only ever outputs 17:00:00repeatedly for every time we had available for testing.
$numberofsecs从阵列中提取的时间在哪里。但这只会在17:00:00我们每次可用于测试时重复输出。
How can I go about making this work correctly?
我怎样才能使这项工作正常进行?
回答by ernie
Assuming that that's a standard unix timestamp string (seconds since midnight 1/1/1970), then you should be able to use dateas you mentioned, but just modify the format string:
假设这是一个标准的 unix 时间戳字符串(自 1970 年 1 月 1 日午夜以来的秒数),那么您应该可以使用您提到的日期,但只需修改格式字符串:
echo date('Y/m/d H:i:s', $numberofsecs);
The example you mention where you were always getting 17:00:00 could have been because your test cases were all only datestamps, encoded as timestamps, and having an offset from GMT . . .
您提到的总是 17:00:00 的示例可能是因为您的测试用例都只是日期戳,编码为时间戳,并且与 GMT 有偏移量。. .
回答by Pragnesh Chauhan
I have tried below code:
我试过下面的代码:
$ts = 1345497551;
$date = new DateTime("@$ts");
echo $date->format('U = Y-m-d H:i:s');
output : 1345497551 = 2012-08-20 21:19:11
输出:1345497551 = 2012-08-20 21:19:11

