convert timestamp string to timestamp integer php [duplicate] - php

This question already has answers here:
Check whether the string is a unix timestamp
(13 answers)
Closed 6 years ago.
Here is my php code
var_dump($args['startdate']);
var_dump((int)$args['startdate']);die();
Here is output
string(13) "1468821556126" int(2147483647)
Expected output
string(13) "1468821556126" int(1468821556126)
Why this is happening and how to resolve this issue ?

You cannot get a reliable result because your number is too large to be represented as an integer.
echo PHP_INT_MAX; //2147483647 on my system
For a number as large as yours, if you need it in numerical format, cast it to a float instead of an integer
echo floatval('1468821556126'); //1.46882155613E+12
From there, if you're trying to get a date, I assume that this is Javascript time, which is in milliseconds. To convert to a Unix Timestamp, you'll then need to divide it by 1000.

It's happening because your $args['startdate'] is in miliseconds, you need to divide it by 1000

You're probably using a 32bit system. The number you're seeing is the maximum size of an int on a 32 bit machine.
Use float or change to a 64bit machine.
Ref: PHP Integer

Related

how to prevent float variables displaying as scientific notation when printing [duplicate]

This question already has answers here:
Why is PHP printing my number in scientific notation, when I specified it as .000021?
(7 answers)
Closed 10 months ago.
I am using "(float)$val" for some calculation, but for some decimal value like -0.00000025478625
(float)-0.00000025478625 is resulting to -2.5479E-70,
i need the value same as that of -0.00000025478625, without affecting other scenarios.
how to prevent this conversion ?
I think you misunderstand the representation of your float. The value -2.5479E-70 actually is still a float value in scientific representation.
What this actually means is that your value is very small, so for readability reasons it is represented in this format. To read it you may replace the E with an multiplication of the following number to the power of 10 -2.5479 * 10^(-70). So this means that your floating point number is prepended with 70 zeros (which I wont write down here).
As example -5.47E-4 would be -5.47 * 10^(-4) which is the same as -5.47/10000 resulting in -0.000547.
Also, for printing your value was rounded. Internally it still uses the exact value. So if you use this number in further evaluations you do not lose any accuracy.
This is the scientific notation of float number. So, if you want to format then you should use number_format() function.
Below example the second parameter will tell at what precision do you need.
So, as per your example you should use 14.
Try this:
$var = number_format((float)-0.00000025478625, 14);
print($var);
Something to add to Manish's answer.
Please note that floating point numbers in PHP have limited precision:
For example:
<?php
echo number_format((float) 0.0000000000000000000000004, 50);
or even
<?php
printf('%f15.50', (float) 0.0000000000000000000000004);
You'd get something like this (depends on the system):
0.00000000000000000000000040000000000000001539794790
which is not exactly the original floating point number to print.
You can never count on the accuracy of floating point number. The best way to deal with them is to always store them as string until you need some calculation done.

how to store huge int value in php,I store 13262075231096090737 in mysql ,but it show as 1.3262075231096E+19 [duplicate]

This question already has answers here:
Turn off scientific notation MySQL
(2 answers)
Closed 3 years ago.
I use php to store a very big int in mysql ,the value is 13262075231096090737 ,but I echo that value,it shows as 1.3262075231096E+19, when I store as varchar,it also show as 1.3262075231096E+19. I want to know how to store a very huge int in mysql??
in your database change field type to 'text' instead of 'varchar'
The problem you are experiencing is not to do with the storage of the BIGINT, but rather the displaying of that number in PHP.
See this post for examples using:
number_format()
sprintf()
printf()
Why is PHP printing my number in scientific notation, when I specified it as .000021?

How To Insert a DECIMAL(a,b) Value Into MYSQL [duplicate]

This question already has answers here:
PHP float/double stored as MySQL DECIMAL
(2 answers)
Closed 8 years ago.
I have a table with a DECIMAL(10,6) column. I have a string representation of a float (e.g. 35.3123122). I am using php to insert the value. I used floatval($string) to insert and it did not work. I also tried to insert the string itself with no success. Do I need to format the float value exactly to match the 10,6 criteria. If so, How can I do this?
Thanks
Not sure if this is your problem, but the value of "35.3123122" has 7 digits after the decimal point. DECIMAL(10,6) is 10 digits in total with a maximum of 6 digits past the decimal point.

Subtract two strings in PHP or mySQL to count differences [duplicate]

This question already has answers here:
Character-wise string diff in PHP
(4 answers)
Closed 9 years ago.
Is it possible to return the number of characters which differ between two strings?
In my case, I would like to run a check to quantify how much a user's draft has changed vs the database version. With this information, the system can decide weather or not the draft needs to be auto-saved.
For example:
echo string_diff("Hello world", "Hi World");
This should output "5", indicating that characters "ello" & "W" are different.
Is this possible with PHP? How about with mySQL?
On the PHP side your options are
similar_text which calculates the percent similarity between two strings
levenshtein - which calculates the edit distance between two strings (the number of key strokes it takes to transform string a into string b)
on mysql you can use this user defined function to calculate the levenshtein distance

How to display/avoid the epsilon value in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Forcing output in standard mathematical notation in PHP
Output displayed: 2e+008
How to avoid displaying the epsilon value in php?
Output needed: 200000000
mysql table
amount float
if i enter 200000000 in input box it stores 2e+008 in mysql and just fetch the value through jquery using this code
$amount=$("[id=amountfee]").val();
<?php echo $amount ?>
You should look at the number_format() function: http://us2.php.net/manual/en/function.number-format.php
use DECIMAL or DOUBLE as data type instead of float. This will also help you to avoid problems you might have on float.

Categories