php 毫秒级时间搓与日期格式互换

/**
     * 将日期转化13位的时间戳
     */
    private function get_data_format($time)
    {
        list($usec,$sec) = explode(".", $time);
        $date = strtotime($usec);
        $return_data = str_pad($date.$sec, 13, "0", STR_PAD_RIGHT);
        return $return_data;
    }
    
    /**
     * 将13位的时间戳转换为日期格式
     */
    private function get_microtime_format($time)
    {
        if(strstr($time,'.'))
        {
            sprintf("%01.3f",$time);
            list($usec,$sec) = explode(".", $time);
            $sec = str_pad($sec,3,"0",STR_PAD_RIGHT);
        }else
        {
            $usec = $time;
            $sec = "000";
        }
        $date = date("Y-m-d H:i:s.x", $usec);
        return str_replace("x", $sec, $date);
    }
    
    /**
     * 返回当前的毫秒时间戳
     */
    private function get_msectime()
    {
        list($msec, $sec) = explode(' ',microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

eg:

$this->get_data_format("2018-07-17 00:00:00.000");

$this->get_microtime_format(1525412810192*0.001);

你可能感兴趣的:(php 毫秒级时间搓与日期格式互换)