Getting unix timestamp in milliseconds in PHP5 and Actionscript3

后端 未结 12 2558
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 09:58

In Actionscript, the Unix timestamp in milliseconds is obtainable like this:

public static function getTimeStamp():uint
        {
            var now:Date =          


        
相关标签:
12条回答
  • 2020-12-16 10:43
    $timestamp = str_replace(".","",number_format((float)microtime(true),2,'.',''));
    
    0 讨论(0)
  • 2020-12-16 10:44

    PHP 7 This function has its return type declared.

    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);
    }
    
    0 讨论(0)
  • 2020-12-16 10:49

    Use this:

    intval(microtime(true)*1000)
    
    0 讨论(0)
  • 2020-12-16 10:55

    when you need the millisecond in str format, I think you should use:

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

    this will fix the bug int some get millisecond example where the milli part is like : 0.056. some example convert the milli part to float, your will get 56 instead of 056. I think some one want 056.

    especially when you need the millisecond to order some data.

    hope will help. :)

    0 讨论(0)
  • 2020-12-16 10:58

    To get millisecond timestamp from PHP DateTime object:

    <?php
    date_default_timezone_set('UTC');
    $d = new \DateTime('some_data_string');
    $mts = $d->getTimestamp().substr($d->format('u'),0,3); // millisecond timestamp
    
    0 讨论(0)
  • 2020-12-16 10:58

    Something like this:

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

    Gives float type representing miliseconds from UNIX epoch to starts of the request.

    0 讨论(0)
提交回复
热议问题