【问题标题】:Getting unix timestamp in milliseconds in PHP5 and Actionscript3在 PHP 5 和 Actionscript 3 中以毫秒为单位获取 unix 时间戳
【发布时间】:2026-02-06 07:05:01
【问题描述】:

在 Actionscript 中,以毫秒为单位的 Unix 时间戳可以这样获得:

public static function getTimeStamp():uint
        {
            var now:Date = new Date();
            return now.getTime();
        }

文档清楚地说明了以下内容:

getTime():Number 返回次数 自 1 月 1 日午夜以来的毫秒数, 1970 年,世界时,日期 对象。

当我跟踪它时,它会返回以下内容:

824655597

所以,824655597 / 1000 / 60 / 60 / 24 / 365 = 0.02 年。 这显然是不正确的,因为它应该是 39 年左右。

问题 #1:这里出了什么问题?

现在,进入 PHP 部分:我也在尝试以毫秒为单位获取时间戳。 microtime() 函数返回字符串 (0.29207800 1246365903) 或浮点数 (1246365134.01),具体取决于给定的参数。因为我认为时间戳很容易,所以我打算自己做。但是现在我已经尝试并注意到了这个浮动,并将它与我在 Actionscript 中的问题结合起来,我真的不知道。

问题 #2:我应该如何让它返回 Unix 时间戳中的毫秒数?

时间戳应该很简单,我可能遗漏了一些东西.. 对此感到抱歉。提前致谢。

EDIT1:自己回答了第一个问题。见下文。
EDIT2:我自己也回答了第二个问题。见下文。无法在 48 小时内接受答复。

【问题讨论】:

    标签: php actionscript-3 unix timestamp


    【解决方案1】:

    我最近遇到了以毫秒为单位获取时间戳的问题。将 unix 时间戳乘以 1000 并不能解决问题,因为我必须非常精确地比较两个数据库条目。显然,php datetime 对象无法处理毫秒/微秒,但无论如何它都存储在 datetime 字符串中。所以这是我的解决方案:

    $dateObject = new \DateTime('2015-05-05 12:45:15.444', new \DateTimeZone('Europe/London')); $millis = $dateObject->format('v'); echo $dateObject->getTimestamp()*1000+$millis;

    如果您使用format->('u')(当然将时间戳乘以 1000000),这也应该适用于微秒。我希望你觉得这很有用。

    【讨论】:

      【解决方案2】:

      PHP 7 这个函数声明了它的返回类型。

      function timestamp_ms(): int {
          $times = gettimeofday();
          $seconds = strval($times["sec"]);
          $milliseconds = strval(floor($times["usec"]/1000));
          $missingleadingzeros = 3-strlen($milliseconds);
          if($missingleadingzeros >0){
              for($i = 0; $i < $missingleadingzeros; $i++){
                  $milliseconds = '0'.$milliseconds;
              }
          }
          return intval($seconds.$milliseconds);
      }
      

      PHP 5

      function timestamp_ms() {
          $times = gettimeofday();
          $seconds = strval($times["sec"]);
          $milliseconds = strval(floor($times["usec"]/1000));
          $missingleadingzeros = 3-strlen($milliseconds);
          if($missingleadingzeros >0){
              for($i = 0; $i < $missingleadingzeros; $i++){
                  $milliseconds = '0'.$milliseconds;
              }
          }
          return intval($seconds.$milliseconds);
      }
      

      【讨论】:

        【解决方案3】:

        从 PHP DateTime 对象获取毫秒时间戳:

        <?php
        date_default_timezone_set('UTC');
        $d = new \DateTime('some_data_string');
        $mts = $d->getTimestamp().substr($d->format('u'),0,3); // millisecond timestamp
        

        【讨论】:

          【解决方案4】:

          当您需要 str 格式的毫秒时,我认为您应该使用:

          public function get_millisecond() {
              list($milliPart, $secondPart) = explode(' ', microtime());
              $milliPart = substr($milliPart, 2, 3);
              return $secondPart . $milliPart;
          }
          

          这将修复错误 int 一些毫秒示例,其中毫秒部分类似于:0.056。一些例子将毫部分转换为浮点数,你会得到 56 而不是 056。我想有人想要 056。

          尤其是当您需要毫秒来订购一些数据时。

          希望会有所帮助。 :)

          【讨论】:

            【解决方案5】:
            $timestamp = str_replace(".","",number_format((float)microtime(true),2,'.',''));
            

            【讨论】:

              【解决方案6】:

              使用这个:

              intval(microtime(true)*1000)
              

              【讨论】:

                【解决方案7】:

                PHP 的简单答案:

                function exact_time() {
                    $t = explode(' ',microtime());
                    return ($t[0] + $t[1]);
                }
                

                【讨论】:

                  【解决方案8】:

                  对于 actionscript3,new Date().getTime() 应该可以工作。


                  在 PHP 中,您可以简单地调用 time() 以获取自 1970 年 1 月 1 日 00:00:00 GMT 以来经过的时间(以秒为单位)。如果您想要毫秒,只需执行(time()*1000)

                  如果您使用microtime(),则将第二部分乘以 1000 得到毫秒。将第一部分乘以 1000 以得到毫秒并四舍五入。然后将这两个数字相加。瞧。

                  【讨论】:

                  • 不会 time()*1000 返回零作为最后 3 位数字吗?
                  • 会的。使用 time() 您可以将时间精确到秒。如果您真的需要毫秒的实际值,请使用 microtime
                  • 自 PHP 5.0.0 起 microtime(true) 以毫秒为单位返回双精度值。
                  • time()*1000 是一个糟糕的答案:-(
                  【解决方案9】:

                  类似这样的:

                  $mili_sec_time = $_SERVER['REQUEST_TIME_FLOAT'] * 1000;

                  给出表示从 UNIX 纪元到请求开始的毫秒数的浮点类型。

                  【讨论】:

                    【解决方案10】:

                    将时间戳规范化为 Javascript、Actionscript 和 PHP 之间以毫秒为单位的整数

                    Javascript/Actionscript:

                    function getTimestamp(){
                        var d = new Date();
                        return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()).valueOf();
                    }
                    

                    PHP:

                    function getTimestamp(){
                        $seconds = microtime(true); // true = float, false = weirdo "0.2342 123456" format 
                        return round( ($seconds * 1000) );
                    }
                    

                    请参阅“ben at Sixg dot com's”的 PHP 注释:http://www.php.net/manual/en/function.gmmktime.php

                    摘录: 对于大多数意图和目的,您可以想象 mktime() 首先将您的输入参数转换为 GMT,然后调用 gmmktime() 来生成 GMT 时间戳。

                    因此,time() 总是会在世界任何地方的同一实际时刻返回相同的内容。
                    gmmktime() 和 mktime(),当给定特定的时间参数时,在计算适当的时间戳之前,将这些时间参数从适当的时区(gmmktime() 为 GMT,mktime() 为本地时间)转换。


                    更新:

                    在某些版本的 PHP 中,以毫秒为单位的时间戳太大而无法显示为字符串。所以使用sprintf函数来获取字符串值:

                    PHP

                    function getTimestamp($asString=false){
                        $seconds = microtime(true); // false = int, true = float
                        $stamp = round($seconds * 1000);
                        if($asString == true){
                            return sprintf('%.0f', $stamp);
                        } else {
                            return $stamp;
                        }
                    }
                    

                    【讨论】:

                      【解决方案11】:

                      我使用无符号整数作为函数的返回类型。这应该是数字。

                      public static function getTimeStamp():Number
                              {
                                  var now:Date = new Date();
                                  return now.getTime();
                              }
                      

                      我想我现在在 PHP5 中获得了毫秒的功能。

                      function msTimeStamp() {
                          return round(microtime(1) * 1000);
                      }
                      

                      【讨论】:

                      • 都对...但是帮自己和他人一个忙,并在 PHP 中将其更改为 microtime(true) ...当然 1 有效,但参数应该是布尔值,仅仅是因为语义...它真的更好阅读...为他人,或为自己几年后... :)
                      • 嗯,虽然我比你更早提供了正确答案,但没有投票赞成?
                      • 请改成微时间(true)
                      【解决方案12】:
                      php5 中的

                      microtime() 根据microtime() 返回带有微秒的 unix 时间戳,如果未提供 get_as_float 参数,它会为您提供格式为“msec sec”的字符串,因此第一部分是毫秒部分,第二部分是第二部分。把它一分为二,你就得到了时间戳的两部分

                      【讨论】:

                        最近更新 更多