32

Is this even possible in PHP?

If not, what is the highest precision available?

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
Matt
  • 11,157
  • 26
  • 81
  • 110
  • Just out of curiosity, why do you need nanosecond precision in PHP? – user229044 Nov 17 '10 at 18:13
  • 2
    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. – Matt Nov 17 '10 at 19:13

3 Answers3

44

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.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • I'm running linux, but for some reason that returns 0. Does it not work on lighttpd? – Matt Nov 17 '10 at 19:11
  • 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. – Matt Nov 17 '10 at 19:21
  • 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. – Matt Nov 17 '10 at 19:25
  • 1
    Got it, I think it should be date +%s%N, because %N doesn't bother showing the seconds part. – Matt Nov 17 '10 at 19:31
  • 7
    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. – Manu Manjunath Nov 22 '13 at 04:21
  • system() is writing to output, exec('date +%s%N') is more correct – andser Oct 16 '15 at 07:22
  • 1
    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. – andser Oct 16 '15 at 07:24
  • Of interest, PHP does implement `time_nanosleep()` which is the only function I know that exposes nanoseconds. – jpschroeder Apr 05 '17 at 02:47
  • system('date +%s%N') makes me like a type of echo, so in a ajax result apear. – Michu Mar 28 '23 at 08:59
17

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.

Anatol Belski
  • 660
  • 6
  • 8
  • It should be the accepted answer since `hrtime()`'s doc says "Returns an array of integers in the form [seconds, nanoseconds]." – Alexandre Huat Aug 25 '22 at 12:05
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.

bcosca
  • 17,371
  • 5
  • 40
  • 51