开发者

Microseconds in PHP

I'm accessing an API that requires a timestamp to be in microseconds, I can print the value and it works it prints this:

1295308800

So I figured I can multiply this by 1000 and then开发者_如何学运维 print this but when I print the query before I send it (using CURL) it prints this:

1.2953088E+12

Can I use a different variable type or something?

Thanks!


If you're sending the value in a way that PHP converts it to its exponent syntax (i.e. 1.2953088E+12) there's a nifty trick you can do using printf or sprintf in order to get the full number):

php -r '$date = 12434613435134661234; echo $date;'
1.2434613435135E+19

php -r '$date = 12434613435134661234; printf("%.0f", $date);'
12434613435134660608

You can then use sprintf to just assign that number to a variable (as a string) and pass that into the remote API.

Note the lack of accuracy from converting the floating point number, though.

Edit:

I also want to point out that PHP's unsigned decimal numbers appear to be accurate up to 19 places. So, this returns the correct value:

php -r '$date = 6243461343513466123; printf("%u", $date);'
6243461343513466123

When expressing that same value as a float, it loses its precision:

php -r '$date = 6243461343513466123; printf("%.0f", $date);'
6243461343513465856


PHP has a built-in microtime() function...

http://php.net/manual/en/function.microtime.php

Depending on exactly what you're trying to do, this may be of use to you.

Here is a StackOverflow post regarding microtime

php microseconds

If you're looking for microseconds of a specific timestamp, then check out the u Format Character here: http://no.php.net/manual/en/function.date.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜