Home  >  Article  >  Backend Development  >  How to Retrieve the Current Time in Milliseconds in PHP?

How to Retrieve the Current Time in Milliseconds in PHP?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-20 12:25:02286browse

How to Retrieve the Current Time in Milliseconds in PHP?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn