Thursday, April 19, 2018

How to get current time in milliseconds in PHP | Converting microtime() to milliseconds/seconds

The short answer is
$milliseconds = round(microtime(true) * 1000);
Use microtime(). This function returns a string separated by a space. The first part is the fractional part of seconds, the second part is the integral part. Pass in true to get as a number:
var_dump(microtime()); // string(21) "0.89115400 1283846202"
var_dump(microtime(true)); // float(1283846202.89)
64 bits platforms only!
function milliseconds() {
    $mt = explode(' ', microtime());
    return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}

No comments:

Post a Comment