microtime

Description

microtime() returns the current Unix timestamp with
microseconds. This function is only available on operating systems that
support the gettimeofday() system call.

Parameters

get_as_float

If used and set to TRUE, microtime() will return a
float instead of a string, as described in
the return values section below.

Return Values

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.

If get_as_float is set to TRUE, then
microtime() returns a float, which
represents the current time in seconds since the Unix epoch accurate to the
nearest microsecond.

// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.// It contains the timestamp of the start of the request with microsecond precision.$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

Note that the timestamp returned is "with microseconds", not "in microseconds". This is especially good to know if you pass 'true' as the parameter and then calculate the difference between two float values -- the result is already in seconds; it doesn't need to be divided by a million.

A lot of the comments here suggest adding in the following way: (float)$usec + (float)$secMake sure you have the float precision high enough as with the default precision of 12, you are only precise to the 0.01 seconds. Set this in you php.ini file. precision = 16

For the microtime expression only the procedural style can be used. If you do not use namespaces the backslashes may be removed.PHP >= 5.4 needed due to accessing a member of a newly created object in a single expression "(new DateTime())->format('Y-m-d H:i:s.u')"

Rather than using the list() function, etc. I have found the following code to be a bit cleaner and simpler:<?php$theTime = array_sum( explode( ' ' , microtime() ) );echo $theTime;# Displays "1212018372.3366"?>

While doing some experiments on using microtime()'s output for an entropy generator I found that its microsecond value was always quantified to the nearest hundreds (i.e. the number ends with 00), which affected the randomness of the entropy generator. This output pattern was consistent on three separate machines, running OpenBSD, Mac OS X and Windows.

The solution was to instead use gettimeofday()'s output, as its usec value followed no quantifiable pattern on any of the three test platforms.

But note that the default 'precision' setting of PHP* - which is used when a float is converted to a stringy format by echo()ing, casting or json_encode()ing etc - is not enough to hold the six digit accuracy of microtime(true).

Out of the box, microtime(true) will echo something like:

1377611450.1234

Which is obviously less than microsecond accuracy. You'll probably want to bump the 'precision' setting up to 16 which will echo something like:

1377611450.123456

*Internally* it will be accurate to the six digits even with the default 'precision', but a lot of things (ie. NoSQL databases) are moving to all-text representations these days so it becomes a bit more important.

However it is true that result depends of gettimeofday() call. ([jamie at bishopston dot net] wrote this & I can confirm)If system time change, result of this function can be unpredictable (much greater or less than zero).

Of the methods I've seen here, and thought up myself, to convert microtime() output into a numerical value, the microtime_float() one shown in the documentation proper(using explode,list,float,+) is the slowest in terms of runtime.

I implemented the various methods, ran each in a tight loop 1,000,000 times, and compared runtimes (and output). I did this 10 times to make sure there wasn't a problem of other things putting a load spike on the server. I'll admit I didn't take into account martijn at vanderlee dot com's comments on testing accuracy, but as I figured the looping code etc would be the same, and this was only meant as a relative comparison, it should not be necessary.

The above method took on average 5.7151877 seconds, while a method using substr and simply adding strings with . took on average 3.0144226 seconds. rsalazar at innox dot com dot mx's method using preg_replace used on average 4.1819633 seconds. This shows that there are indeed differences, but for normal use noone is going to notice it.

Note that the substr method mentioned isn't quite the one given anonymously below, but one I made based on it:<?php$time=microtime();$timeval=substr($time,11).substr($time,1,9);?>

Also worth noting is that the microtime_float() method gets faster, and no less accurate, if the (float) conversions are taken out and the variables are simply added together.

Any of the methods that used + or array_sum ended up rounding the result to 2 digits after the decimal point, while (most of) the ones using preg_replace or substr and . kept all the digits.

For accurate timing, since floating-point arithmetic would lose precision, I stored microtime results as-is and calculated time difference with this function:<?phpfunction microtime_used($before,$after) { return (substr($after,11)-substr($before,11)) +(substr($after,0,9)-substr($before,0,9));}?>