Retrieving the Current Time in Milliseconds with PHP
In PHP, the time() function returns the current time in seconds. However, for scenarios where greater precision is required, it may be necessary to obtain the current time in milliseconds.
Solution:
The solution involves utilizing microtime(true). This function returns the current time measured in seconds and microseconds, with microseconds representing the fractional part. To convert this value to milliseconds:
<code class="php">$microseconds = microtime(true); $timestamp = floor($microseconds * 1000); // Convert to milliseconds</code>
Example:
<code class="php">$timestamp = floor(microtime(true) * 1000); echo 'Timestamp in milliseconds: ' . $timestamp . '<br>';</code>
Output:
Timestamp in milliseconds: 1631685547387
By utilizing this approach, you can retrieve the current time in milliseconds with greater accuracy compared to using time().
The above is the detailed content of How to Retrieve the Current Time in Milliseconds in PHP?. For more information, please follow other related articles on the PHP Chinese website!