Convert String to int in PHP

$date = "1346706576967"; // miliseconds $newDate = (int) $date; echo $newDate; 

I get "2147483647" as $ newDate.

I just want to convert a variable from String 1346706576967 to int 1346706576967 - how is this possible?

+4
source share
4 answers

2147483647 is the largest value, to which, unfortunately, an integer refers. You can use float instead, since float can store integer values ​​exactly up to 1,000,000,000,000

+5
source

Because this is the maximum size an integer can have in PHP. You will need a special PHP library designed to work with large integers such as BCMath or GMP, or just convert it to a float.

+2
source

possible conversions

 $input => 1346706576967 (integer)$input => 2147483647 intval($input) => 2147483647 $input*1 => 1346706576967 settype($input, "integer") => 1346706576967 

http://phpconvert.com/online/

+1
source

You can use implicit conversion to convert it correctly:

 $date = "1346706576967"; // miliseconds $newDate = 0+$date; // float(1346706576967) $newDate = (int) $date; // int(2147483647) 
0
source

Source: https://habr.com/ru/post/1433002/


All Articles