问题
I am pulling data from an JSON database that is storing the time in unix epoch but for some reason the numbers are being floated when I'm pulling them out. This is something I've never had to deal with before. So basiclly a number like 1293083730000
is showing up as 1.293085408E+12
I need to get the number back to the epoch time so I can compare it to the current time. Any help would be great.
回答1:
That's engineering notation, a convenient method of writing large numbers. The number is still an integer.
The problem is is that PHP's internal types are too small to represent the number as a decimal, see the following example:
<?
$i = 1293083730000;
echo "\$i is $i\n\n";
echo sprintf("\$i is %d\n\n", $i);
echo sprintf("\$i is %e\n\n", $i);
?>
This outputs:
$i is 1293083730000
$i is 298573904
$i is 1.293084e+12
You need either a 64-bit platform or work with the number as a string or floating point value. See the PHP documentation on Integers for more details:
http://php.net/manual/en/language.types.integer.php
回答2:
In the php.ini
configuration file, there is a value 'precision'
.
It simply defines how many digits will be shown in a float number.
More informations in PHP manual
You can increase the precision value and try again.
来源:https://stackoverflow.com/questions/4516312/float-to-unix-epoch