Xaver
Xaver

Reputation: 11682

strtotime doesn't return false with a valid timestamp

Any idea why this

strtotime('1356532858')  //Wed, 26 Dec 2012 14:40:58 GMT

returns 28043243813 while

strtotime('1356055871')  //Fri, 21 Dec 2012 02:11:11 GMT

returns false?

I'm searching for a solution where I can translate any common date string into a valid timestamp. Unfortunately there are some (valid) timestamps included as well

Upvotes: 1

Views: 135

Answers (2)

Jeff Lambert
Jeff Lambert

Reputation: 24661

Try putting a '@' in front of your timestamp. This will instruct strtotime that this is a timestamp in Unix format:

var_dump(strtotime('@1356055871'));

Source

Upvotes: 1

lpg
lpg

Reputation: 4937

maybe you could try to use strtotime only if string is not 10-digit?

$str = '1356055871';
$timestamp = (preg_match('/\d{10}/',$str,$match))?(int)$str:strtotime($str);
echo $timestamp;

Regarding the question why you receive a value on first execution, and false in second one, this is because 1356532858 is interpreted as some valid date, although is not what you're expecting:

echo date("Y-m-d H:i:s",strtotime('1356532858'));
// outputs:   2858-08-27 13:56:53

strtotime can accept your timestamp, but should have a leading @:

echo date("Y-m-d H:i:s",strtotime('@1356532858'));
// outputs:   2012-12-26 15:40:58

Upvotes: 1

Related Questions