time()
is in seconds - is there one in milliseconds?
microtime() returns the number of seconds since the "epoch time" with precision up to microseconds with two numbers separated by space, like... The second number is the seconds (integer) while the first one is the decimal part. Note that both $mt[1] and the result of round are casted to int .
The microtime() function returns the current Unix timestamp with microseconds.
The Date. now() and new Date(). getTime() calls retrieve the milliseconds since the UTC epoch. Convert the milliseconds since epoch to seconds by dividing by 1000.
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)
Beware of precision loss if you use microtime(true)
.
There is also gettimeofday
that returns the microseconds part as an integer.
var_dump(gettimeofday()); /* array(4) { ["sec"]=> int(1283846202) ["usec"]=> int(891199) ["minuteswest"]=> int(-60) ["dsttime"]=> int(1) } */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With