Praveen Kumar Praveen Kumar - 4 months ago 30
PHP Question

convert timestamp string to timestamp integer php

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 ?

Answer

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.