/    Sign up×
Community /Pin to ProfileBookmark

microtime(true): what does this return

Hi. I read the manual, but I still have a question. If I do
$t = microtime(TRUE);
Then $t contains the “current time in seconds since the Unix epoch accurate to the nearest microsecond”

So if I want $t to contain the number of microseconds since the Unix epoch, do I do this?
$t = round(1000000*microtime(TRUE));

The reason I ask is that everytime I do this, I get an integer where the last two digits are always zero. So this makes me think that $t is accurate to the nearest 100 microseconds, or in otherwords, microtime(TRUE) is returning only 4 digits of precision.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 17.2012 — My first thought is that you may be blowing the max integer size on your PHP installation if you mutliply a timestamp integer (which it 2038 will get too big for a 32-bit system) by 1000000. You might be better off leaving it as a floating point with a decimal portion, or else use something like the BCMath or GMP extension to allow you to work with arbitrary-length integers.
Copy linkTweet thisAlerts:
@PacopagauthorMay 17.2012 — Great thought. I'm using Arch Linux 64 bit. It was actually working at one point, where all digits were generally non-zero. It seems like things suddenly changed at some point in time. It may be related to a recent upgrade to php5.4 when I had to reconfigure php.ini. Is there anything in php.ini that could reflect this behaviour?

Using floats would probably be a better solution. The reason I haven't done this is that I don't like dealing with floats in mysql. I would need to use an exact type, like DECIMAL, but I was having trouble with it. What data type would you use for a field in which you would want to store the output of microtime(TRUE) ?
Copy linkTweet thisAlerts:
@NogDogMay 17.2012 — If you need to avoid floating point errors, then DECIMAL is probably the best fit. You'd probably want 16 significant digits with 6 decimal places, e.g. DECIMAL(16, 6) (or maybe 17,6 to be on the safe side ? ).
Copy linkTweet thisAlerts:
@PacopagauthorMay 17.2012 — So it turns out that I was having problems with my brain the last time I tried using decimal in mysql. The correct specification would be DECIMAL(16,6).

And I found a parameter called "precision" in php.ini. In my default configuration it is set to 14 (must've happened during the last php upgrade). This is the number of significant digits. So what I was getting was the 10 digit timestamp plus another 4 digits for the "microsecond" part, totalling 14.

Edit: I was too slow. Thanks a bunch. I'm gonna stick with decimal and leave microtime(TRUE) as a float. Should work fine now. Thanks.
×

Success!

Help @Pacopag spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...