将unix时间戳转换为Php中的日期

问题描述 投票:1回答:2

发布此帖子之前,我已经在stackoverflow中提到了问题。就我而言,我需要将1426023505154转换为日期格式。为确保时间戳有效,我在http://www.epochconverter.com中进行了检查。我用这些代码进行转换。但这不起作用:

date_default_timezone_set('Asia/Calcutta');
$date = new DateTime();
$date->setTimestamp(1426023505154);
echo $date->format('U = Y-m-d H:i:s') ;
echo gmdate("Y-m-d\TH:i:s\Z", 1426023505154);
echo echo date('d-m-Y', 1426023505154);

但是所有结果都是错误的,例如:

  1. 47158-11-20 21:49:14

  2. 20-11-47158

  3. 47158-11-20T16:19:14Z

请让我知道如何解决此问题。谢谢

php zend-framework2 unix-timestamp
2个回答
2
投票

问题是您的时间戳以毫秒为单位,$date->setTimestamp使用秒,您可以通过除以1000来解决此问题

$date = new DateTime();
$value = 1426023505154;
$date->setTimestamp($value/1000);

1
投票

尝试-->

echo date('Y-m-d H:i:s', 1426023505154 / 1000 );
// dividing by 1000 because the timestamp is in microseconds
© www.soinside.com 2019 - 2024. All rights reserved.