php method to get the number of milliseconds of the current time

  • 2020-12-21 17:59:33
  • OfStack

php itself does not provide a function that returns the number of milliseconds, but does provide an microtime() function that returns an array containing two elements, one for seconds and one for milliseconds as a decimal. With this function, it is easy to define a function that returns the number of milliseconds, such as:
 
function getMillisecond() { 
list($s1, $s2) = explode(' ', microtime()); 
return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); 
} 

Note that on 32-bit systems, the int maximum value for php is much smaller than the number of milliseconds, so you cannot use the int type, and php does not have the long type, so you have to use floating point numbers instead. Because of the use of floating point numbers, if the accuracy is not set correctly, it may be incorrect to display the obtained results using echo, and the accuracy setting must not be less than 13 bits to see the correct output.

Related articles: