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


当前回答

$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
    ($the_date_time->format('u') / 1000);

其他回答

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

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

简单的回答是:

$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 

用这个: 函数get_millis () { List ($usec, $sec) =爆炸(' ',microtime()); 返回(int) ((int) $sec * 1000 + ((float) $usec * 1000)); }

Bye

在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);
}