【问题标题】:Linux timestamp to PHPLinux时间戳到PHP
【发布时间】:2012-07-14 19:54:33
【问题描述】:

我有以下时间戳:

1342259667654

当使用http://www.epochconverter.com/ 转换时会给出:

Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2

这是正确的时间,但是使用时:

echo date("Y-m-d H:i:s", 1342259667654);

我得到以下日期:

1904-07-24 10:22:47

如何使用 PHP 获取该时间戳之外的确切日期?

【问题讨论】:

  • 你想要当前日期还是什么?
  • 仅删除最后 3 个字符就可以得到正确的结果?即 1342259667
  • DERP... 因为那个时间戳是毫秒,PHP date() 用的是秒,所以需要除以1000
  • 您的时间戳中的数字过多。 134225966765413 数字,而当前时间戳1342296092 只有10

标签: php timestamp


【解决方案1】:

你的时间戳需要除以 1000:

echo date("Y-m-d H:i:s", 1342259667654/1000);

【讨论】:

  • 谢谢@davidethell,错过了那部分。
【解决方案2】:
$timestamp = 1342259667; 
$dt = new DateTime("@$timestamp");  // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');

你也可以这样做。

【讨论】:

    【解决方案3】:

    1342259667654 的值实际上是以毫秒为单位的,而 PHP 的 date() 函数无法处理毫秒值。因此奇怪的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 2012-08-15
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多