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


当前回答

简单的回答是:

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

其他回答

简单的回答是:

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

在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);
}
$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
    ($the_date_time->format('u') / 1000);

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

输出:

2016-11-19 15:12:34.346351

试试这个:

public function getTimeToMicroseconds() {
    $t = microtime(true);
    $micro = sprintf("%06d", ($t - floor($t)) * 1000000);
    $d = new DateTime(date('Y-m-d H:i:s.' . $micro, $t));

    return $d->format("Y-m-d H:i:s.u"); 
}