Marius S
Marius S

Reputation: 287

Convert String Timestamp to timestamp in PHP

I have a string like "1427835599999" that I want to convert to timestamp so I can use it in the date() function.

So far I have tried these:

$time = date("Y-m-d", $timestamp);

which returns an error ($timestamp is expected to be a long, string given);

$time = date("Y-m-d", strtotime($timestamp));

which returns 01-01-1970

So I need to convert my string to long so I can use it in the date function.

Thanks

EDIT:

(int)$timestamp is not working either. $timestamp+0 is not working.

Upvotes: 1

Views: 1442

Answers (1)

n-dru
n-dru

Reputation: 9420

It looks as a product of javascript's .getTime() function, returning number of milliseconds since 1970/01/01, hence 13 digits as opposed to timestamp being the number of seconds, so having 10 digits. Just divide it by 1000:

$time = date("Y-m-d", $timestamp/1000);
echo $time; // output: 2015-03-31

Upvotes: 2

Related Questions