【问题标题】:Convert UNIX timestamp to milliseconds将 UNIX 时间戳转换为毫秒
【发布时间】:2011-06-21 18:23:59
【问题描述】:

我如何使用 PHP 来获得一个 UNIX 时间戳,就像我从 JS 方法 .getTime() 获得的那样?我似乎遇到了麻烦,因为.getTime() 返回毫秒。我知道我必须先转换时间戳才能让 JS 读取它,但我该怎么做呢?

编辑:

同意乘以1000,但为什么会得到这个?:

timestamp: 1305593400
timestamp * 1000: 1.3055934E+12

timestamp: 1305612420
timestamp * 1000: 1.30561242E+12

timestamp: 1305635400
timestamp * 1000: 1.3056354E+12

timestamp: 1304901960
timestamp * 1000: 1.30490196E+12

timestamp: 1304944620
timestamp * 1000: 1.30494462E+12

【问题讨论】:

    标签: php unix-timestamp gettime


    【解决方案1】:

    UNIX 时间戳以秒为单位。乘以 1000。

    【讨论】:

    • 新数字太大而无法放入 int。但它们仍然是合适的数字。
    • 但是我的 JS 代码无法正确读取它,但是使用正确的 JS getTime() 时间戳可以正常工作,但不是这样。所以这并不能完全回答我的问题。
    • 在这里工作正常。 js> Date(1.3055934E+12)Tue Jun 21 2011 14:35:12 GMT-0400 (EDT)
    【解决方案2】:

    如果您真的需要适当的演示,请使用number_format()

    $timestamp = 1305593400;
    $ms = $timestamp * 1000;
    echo number_format($ms, 0, '.', '');
    

    结果: 1305593400000

    【讨论】:

      【解决方案3】:

      我用过

      $unix_date = (time("Ymd", strtotime($r->date)) *1000);

      【讨论】:

        猜你喜欢
        • 2018-10-10
        • 1970-01-01
        • 2016-03-04
        • 2013-03-09
        • 2014-04-17
        • 2016-03-04
        • 2017-10-11
        相关资源
        最近更新 更多