TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Converting String to int in PHP

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

I am getting "2147483647" as $newDate.

I simply want to convert the variable from String 1346706576967 to int 1346706576967 - how is this possible?

Upvotes: 4

Views: 1288

Answers (4)

UserBSS1
UserBSS1

Reputation: 2211

possible conversions,

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

http://phpconvert.com/online/

Upvotes: 1

Stano
Stano

Reputation: 8939

You can use implicit conversion to convert it correctly:

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

Upvotes: 0

Louis Huppenbauer
Louis Huppenbauer

Reputation: 3714

Because that is maximum size an integer in PHP can have. You'll need a PHP library specific made for dealing with bigger integers like BCMath or GMP or just convert it to a float.

Upvotes: 2

Will Sampson
Will Sampson

Reputation: 514

2147483647 is the largest value an integer can hold unfortunately. You could use a float here instead as a float can accurately hold integer values up to 10000000000000

Upvotes: 5

Related Questions