Get current timestamp with milliseconds
It can print less digits, because it is a float value. So if you get 1234.0005
, that `.0005 actually means 500 microseconds. The zeroes after the 5 are lost because it's still an unformatted float value.
The function microtime
is based on the system call gettimeofday()
, which also isn't accurate to the microsecond. Commonly it's accurate to 10 microseconds. See also Linux - Is gettimeofday() guaranteed to be of microsecond resolution.
-edit- I see the specs in your question have changed from microseconds to milliseconds.
To get the float value you have as an integer, you can multiply by a value. The float value represents seconds. Multiply by 1000 to get milliseconds or 1,000,000 to get microseconds. Since the specs (now) say milliseconds, you should multiply by 1000. 10k will give you accuracy of 1/10ms = 100μs. A millisecond is one thousandth of a second. A microsecond is one millionth of a seconds.
Long story short, to get the time in integer milliseconds, use this:
$milliseconds = intval(microtime(true) * 1000);
Note: there is a reason why you get the time as a string or a float by default. The reason is that on 32 bit systems, PHP's integer is also 32 bits and is not large enough to contain the timestamp including milliseconds and microseconds. So this solution will only work well on a 64 bit system.
$timeStampData = microtime();
list($msec, $sec) = explode(' ', $timeStampData);
$msec = round($msec * 1000);
$result = $sec . $msec;
Something like this
But note, that js date.now() return time in MILLIseconds, not MICROseconds