❮ PHP Date/Time Reference
Example
Return the current Unix timestamp with microseconds:
Try it Yourself »
Definition and Usage
The microtime[] function returns the current Unix timestamp with microseconds.
Syntax
Parameter Values
return_float | Optional. When set to TRUE it specifies that the function should return a float, instead of a string. Default is FALSE |
Technical Details
Returns the string "microsec sec" by default, where sec is the number of seconds since the Unix Epoch [0:00:00 January 1, 1970 GMT], and microsec is the microseconds part. If the return_float parameter is set to TRUE, it returns a float representing the current time in seconds since the Unix epoch accurate to the nearest microsecond |
4+ |
PHP 5.0: Added the return_float parameter |
❮ PHP Date/Time Reference
[PHP 4, PHP 5, PHP 7, PHP 8]
microtime — Return current Unix timestamp with microseconds
Description
microtime[bool $as_float
= false
]: string|float
For performance measurements, using hrtime[] is recommended.
Parameters
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 as a decimal fraction.
If 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.
Examples
Example #1 Timing script execution
Example #2 microtime[] and REQUEST_TIME_FLOAT
See Also
- time[] - Return current Unix timestamp
- hrtime[] - Get the system's high resolution time
Short answer:
64 bits platforms only!
function milliseconds[] {
$mt = explode[' ', microtime[]];
return [[int]$mt[1]] * 1000 + [[int]round[$mt[0] * 1000]];
}
[ If you are running 64 bits PHP then the constant PHP_INT_SIZE
equals to 8
]
Long answer:
If you want an equilvalent function of time[]
in milliseconds first you have to consider that as time[]
returns the number of seconds elapsed since the "epoch time" [01/01/1970], the number of milliseconds since the "epoch time" is a big number and doesn't
fit into a 32 bits integer.
The size of an integer in PHP can be 32 or 64 bits depending on platform.
From //php.net/manual/en/language.types.integer.php
The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value [that's 32 bits signed]. 64-bit platforms usually have a maximum value of about 9E18, except for Windows, which is always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
If you have 64 bits integers then you may use the following function:
function milliseconds[] {
$mt = explode[' ', microtime[]];
return [[int]$mt[1]] * 1000 + [[int]round[$mt[0] * 1000]];
}
microtime[]
returns the number of seconds since the "epoch time" with precision up to microseconds with two numbers separated by space, like...
0.90441300 1409263371
The second number is the seconds [integer] while the first one is the decimal part.
The above function milliseconds[]
takes the integer part multiplied by 1000
1409263371000
then adds the decimal part multiplied by 1000
and rounded to 0 decimals
1409263371904
Note that both $mt[1]
and the result of round
are casted to int
. This is necessary because they are float
s and the operation on them without casting would result in the function returning a float
.
Finally, that function is slightly more precise than
round[microtime[true]*1000];
that with a ratio of 1:10 [approx.] returns 1 more millisecond than the correct result. This is due to the limited precision of the float type [microtime[true]
returns a float]. Anyway if you still prefer the shorter round[microtime[true]*1000];
I would suggest casting to int
the result.
Even if it's beyond the scope of the question it's worth mentioning that if your platform supports 64 bits integers then you can also get the current time in microseconds without incurring in overflow.
If fact 2^63 - 1
[biggest signed integer] divided by 10^6 * 3600 * 24 * 365
[approximately the microseconds in one year] gives 292471
.
That's the same value you get with
echo [int][ PHP_INT_MAX / [ 1000000 * 3600 * 24 * 365 ] ];
In other words, a signed 64 bits integer have room to store a timespan of over 200,000 years measured in microseconds.
You may have then
function microseconds[] {
$mt = explode[' ', microtime[]];
return [[int]$mt[1]] * 1000000 + [[int]round[$mt[0] * 1000000]];
}