Search code examples
phpdatetimenanotime

How to get time in PHP with nanosecond precision?


Is this even possible in PHP?

If not, what is the highest precision available?


Solution

  • 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.