How to get time in PHP with nanosecond precision?
Asked Answered
T

3

33

Is this even possible in PHP?

If not, what is the highest precision available?

Trager answered 17/11, 2010 at 17:35 Comment(2)
Just out of curiosity, why do you need nanosecond precision in PHP?Outmaneuver
I'm doing something involving time synchronisation, where a program asks the remote server for it's time. Java can give the time in nanoseconds, so I thought it would be cool if I could get PHP to do that too rather than wasting the extra precision.Trager
A
45

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.

Allrud answered 17/11, 2010 at 17:37 Comment(9)
I'm running linux, but for some reason that returns 0. Does it not work on lighttpd?Trager
I just noticed and fixed that after googling date :P But something strange is happening, it just returns seemingly random 9 digit numbers like 519716810. It does the same when I run the command in SSH, so I don't think it's a data type issue with PHP.Trager
If I run the command a few times in a for loop, the numbers increase from 0........ to 9........, and then back again. So it does seem like the first half of the number is missing.Trager
Got it, I think it should be date +%s%N, because %N doesn't bother showing the seconds part.Trager
Just out of curiosity: A system call like system need to spawn a shell and return output to PHP. Wouldn't that take a lot more than a nanosecond? If so, wouldn't it defeat the purpose of find time to nanosecond precision? Please advice.Perineuritis
system() is writing to output, exec('date +%s%N') is more correctProtestant
Think twice, do you really need nanoseconds, becuase there's performance drop: exec('date +%s%N'): 3.1263501644135 seconds; round(microtime(true) * 1000): 0.0062530040740967 seconds; Tested on 1000 cycles with calling this functions.Protestant
Of interest, PHP does implement time_nanosleep() which is the only function I know that exposes nanoseconds.Cyanotype
system('date +%s%N') makes me like a type of echo, so in a ajax result apear.Milford
C
18

Now with this extension it's nanosecond timing is possible http://pecl.php.net/hrtime . Yet it is a stopwatch class implemented to suite the highest possible resolution on different platforms.

As of PHP 7.3 there's also a crossplatform hrtime() function in the core http://php.net/manual/en/function.hrtime.php.

Couch answered 18/5, 2014 at 9:35 Comment(1)
It should be the accepted answer since hrtime()'s doc says "Returns an array of integers in the form [seconds, nanoseconds]."Clack
M
14

microtime() is the highest precision using PHP's vocabulary. You can also make a system call by using Unix date and specify %N format if you need nanosecond accuracy. Unix ntp_gettime() has even higher resolution and returns jitter/wander/stability/shift/calibration data.

Melanous answered 17/11, 2010 at 17:37 Comment(1)
Ah, I was hoping there would be something higher than that =[Trager

© 2022 - 2024 — McMap. All rights reserved.