How To Get Current Time In Milliseconds In Php?

Use microtime. This function returns a string separated by a space. The first part is the fractional part of seconds, the second part is the integral part. Pass in true to get as a number:

var_dump(microtime()); // string(21) "0.89115400 1283846202"var_dump(microtime(true)); // float(1283846202.89)Beware of precision loss if you use microtime(true).

Bạn đang xem: How to get current time in milliseconds in php?

There is also gettimeofday that returns the microseconds part as an integer.

var_dump(gettimeofday());/*array(4) <"sec">=> int(1283846202) <"usec">=> int(891199) <"minuteswest">=> int(-60) <"dsttime">=> int(1)*/
Improve sầu this answer
edited Mar 26 "19 at 16:56


Alessio Cantarella
4,58133 gold badges20trăng tròn silver badges2929 bronze badges
answered Sep 7 "10 at 7:50

463k9393 gold badges10121012 silver badges971971 bronze badges
| Show 3 more comments

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 khổng lồ 8 >

Long answer:

The size of an integer in PHPhường. can be 32 or 64 bits depending on platsize.


The kích cỡ 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 kích cỡ can be determined using the constant PHP_INT_SIZE, & maximum value using the constant PHP_INT_MAX since PHP 4.4.0 & PHP 5.0.5.

If you have sầu 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 khổng lồ microseconds with two numbers separated by space, lượt thích...

Xem thêm: Can'T Download In Vmos 1, Vmos Virtual Machine Operating System

0.90441300 1409263371The 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

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 khổng lồ 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 platsize 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 lớn 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));