Get current timestamp in milliseconds [duplicate]
Asked Answered
T

3

11

I want to get current timestamp with milliseconds in PHP something like below, in JavaScript I use Date.now()

1436030635348

I tried something like below:

$time = str_replace(".","",microtime(true)); 

But sometime it is not working properly. Like it prints one or two digits less.

Trescott answered 4/7, 2015 at 17:29 Comment(4)
do you need the timestamp in milliseconds or in microseconds? the title says one thing and the actual question anotherBrusque
it is rounding problem. Trailing zeros are not echoed, str_pad (str_replace(".","",microtime(true)) , 14, 0 )Scrod
@AlexAndrei : sorry i need in milli secondsTrescott
Then considering the result of microtime() is in second, all you need to do to convert it to the millisecond by multiplying it by a 1000 :)Biskra
F
16

It can print less digits, because it is a float value. So if you get 1234.0005, that `.0005 actually means 500 microseconds. The zeroes after the 5 are lost because it's still an unformatted float value.

The function microtime is based on the system call gettimeofday(), which also isn't accurate to the microsecond. Commonly it's accurate to 10 microseconds. See also Linux - Is gettimeofday() guaranteed to be of microsecond resolution.

-edit- I see the specs in your question have changed from microseconds to milliseconds.

To get the float value you have as an integer, you can multiply by a value. The float value represents seconds. Multiply by 1000 to get milliseconds or 1,000,000 to get microseconds. Since the specs (now) say milliseconds, you should multiply by 1000. 10k will give you accuracy of 1/10ms = 100μs. A millisecond is one thousandth of a second. A microsecond is one millionth of a seconds.

Long story short, to get the time in integer milliseconds, use this:

$milliseconds = intval(microtime(true) * 1000);

Note: there is a reason why you get the time as a string or a float by default. The reason is that on 32 bit systems, PHP's integer is also 32 bits and is not large enough to contain the timestamp including milliseconds and microseconds. So this solution will only work well on a 64 bit system.

Flossieflossy answered 4/7, 2015 at 17:34 Comment(7)
so how can I preserve zero .. and any other function to get timestamp without decimal ?Trescott
Instead of replacing the . you can multiply by a million.Flossieflossy
lol million or 10k ?10k seems to workTrescott
A million for microseconds. I see you've changed the specs to milliseconds, so then you should divide by 1000. 10k will give you accuracy of 1/10ms = 100μs. A millisecond is one thousandth of a second. A microsecond is one millionth of a seconds. I've updated the answer.Flossieflossy
So in that case my answer was correct right?Agulhas
Why do I get output like 1.47658187028E+12? I changed to intval(microtime(true)*1000) and it is fine now.Virgule
@JamesWayne You get that output, because the result of round is a float, which tends to be displayed this way if its very large. The (small) difference is that round rounds, while intval truncates, so the outcome between the two could differ one millisecond, but that's probably not a big issue.Flossieflossy
H
1
$timeStampData = microtime();
list($msec, $sec) = explode(' ', $timeStampData);
$msec = round($msec * 1000);

$result = $sec . $msec;

Something like this

But note, that js date.now() return time in MILLIseconds, not MICROseconds

Haskins answered 4/7, 2015 at 17:44 Comment(4)
so how to get timestamp in milliseconds in php ?Trescott
yea i need in milli secondsTrescott
example above show how to get it. if u want u get timestamp with microseconds - you should change this round($msec * 1000) to round($msec * 1000000)Haskins
micro means 10^-6, milli - 10^-3Haskins
A
0

I think you just need to use this function to get a Timestamp with micro seconds in PHP:

int microtime ( void )

Use it as shown in the following link, following code snippet shows you how I'd altered it according to be helpful to your question:

<?php

function microtime_microseconds()
{
     list($usec, $sec) = explode(" ", microtime());
     return round(($usec * 1000) + $sec);
}

$micro_time = microtime_microseconds();

echo $micro_time;

?>

And also for reference regarding to that microtime() PHP function, please visit and read this PHP manual page.

Agulhas answered 4/7, 2015 at 17:36 Comment(5)
Correct, have a look at php's microtime functionBiskra
@Biskra thanks Ali, I updated the answer according to your suggestion.Agulhas
That's the same function OP is using. microtime returns either a float or a string. There is no int. The w3schools example you linked to uses the string version to return a float. This is the replacement for old versions of PHP that didn't support get_as_float = true as a parameter to microtime. So, this answer doesn't solve OP's problem in any way.Flossieflossy
@Flossieflossy I understand what you said. But can you recheck the answer as I edited it once again!Agulhas
Still not right, unfortunately. You need to multiply by a million to get microseconds (or change the name of the function to microtime_milliseconds). More importantly, you need to multiply the seconds too to get the right results. And then still, it's easier really to use the float value like I did in my answer.Flossieflossy

© 2022 - 2024 — McMap. All rights reserved.