Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get time in PHP with nanosecond precision?

Is this even possible in PHP?

If not, what is the highest precision available?

like image 276
Matt Avatar asked Nov 17 '10 17:11

Matt


People also ask

How to get timestamp in PHP?

The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Is timestamp in seconds or milliseconds PHP?

The microtime() function returns the current Unix timestamp with microseconds.

Is microtime in seconds?

By default, microtime() returns a string in the form "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT), and msec measures microseconds that have elapsed since sec and is also expressed in seconds as a decimal fraction.

How can get current date and time in milliseconds PHP?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger.


1 Answers

The microtime function is what you're looking for.

PHP does not supply a function that has higher precision than microseconds.

You can use the system function to get the value straight from the machine if you are running Linux:

$nanotime = system('date +%s%N'); 

%s is the amount of seconds, appended by %N, which is the amount of nanoseconds.

like image 179
Jacob Relkin Avatar answered Sep 22 '22 22:09

Jacob Relkin