Time()是以秒为单位的——有以毫秒为单位的吗?


当前回答

echo date('Y-m-d H:i:s.')。gettimeofday()(“购买”);

输出:

2016-11-19 15:12:34.346351

其他回答

如果你想看到真正的微秒,你需要将php.ini中的精度设置更改为16。

在此之后,microsecond(true)为我提供了1631882476.298437的输出。

所以我认为我需要用余数(298437)除以1000,但实际上,余数是0.298437秒。所以我需要把它乘以1000才能得到正确的结果。

    function get_milliseconds()
    {
        $timestamp = microtime(true);
        return (int)(($timestamp - (int)$timestamp) * 1000);
    }

这是我的实现,应该工作在32位以及。

function mstime(){
    $mstime = explode(' ',microtime());
    return $mstime[1].''.(int)($mstime[0]*1000);
}
$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
    ($the_date_time->format('u') / 1000);
$timeparts = explode(" ",microtime());
$currenttime = bcadd(($timeparts[0]*1000),bcmul($timeparts[1],1000));
echo $currenttime;

注意:该功能需要PHP5,因为 Microtime()和BC数学模块也是必需的(因为我们正在处理 对于较大的数字,可以检查phpinfo中是否有该模块)。

希望这对你有帮助。

字符串变体的最短版本(32位兼容):

$milliseconds = date_create()->format('Uv');