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


当前回答

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

输出:

2016-11-19 15:12:34.346351

其他回答

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

function mstime(){
    $mstime = explode(' ',microtime());
    return $mstime[1].''.(int)($mstime[0]*1000);
}

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

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

在PHP 5中使用microtime(true),或者在PHP 4中进行以下修改:

array_sum(explode(' ', microtime()));

编写该代码的可移植方法是:

function getMicrotime()
{
    if (version_compare(PHP_VERSION, '5.0.0', '<'))
    {
        return array_sum(explode(' ', microtime()));
    }

    return microtime(true);
}

简单的回答是:

$milliseconds = floor(microtime(true) * 1000);

PHP 5.2.2 <

$d = new DateTime();
echo $d->format("Y-m-d H:i:s.u"); // u : Microseconds

PHP 7.0.0 < 7.1

$d = new DateTime();
echo $d->format("Y-m-d H:i:s.v"); // v : Milliseconds