This article demonstrates how to get the current time in milliseconds in PHP.

1. Using microtime() function

You can use the microtime() function to get the current Unix timestamp in microseconds. It returns a string separated by a space, like "msec sec", where the sec is the number of seconds that have elapsed since Epoch and the msec is the fractional part of the sec with microseconds precision. The example output is string(21) "0.52257400 1576512483".

To get the current time in milliseconds, you can do like:

Download  Run Code

 
The microtime() function takes an optional boolean parameter, which makes the function return a float instead of a string when set. The float value represents the current time in seconds, accurate to the nearest microsecond. The example output is float(1673523844.522574)

Download  Run Code

2. Using gettimeofday() function

You may also call the gettimeofday() function to get the current time. It returns an array with sec, usec, minuteswest, and dsttime as keys. Sample output is like:

array(4) {
  ["sec"]=>int(1576512483)
  ["usec"]=>int(522574)
  ["minuteswest"]=>int(-60)
  ["dsttime"]=>int(0)
}

Here, sec and usec represent the seconds since the Unix epoch and microseconds, respectively. These values can be used as follows to get the current time in milliseconds:

Download  Run Code

 
Similar to the microtime() function, gettimeofday() takes an optional boolean parameter indicating whether the function should return a float value rather than a string. The example output is float(1673523844.522574).

Download  Run Code

That’s all there is to getting the current time in milliseconds in PHP.