Convert Epoch Time to Date PHP

user1701252 picture user1701252 · Nov 20, 2012 · Viewed 70.1k times · Source

I'm using an API right now and it provides an epochTime. I've tried everything to convert this epochtime to date, but it doesn't seem to be working including $epoch_time / 1000 and then using the date() function to convert it.

The epoch time looks something like this 1353430853299. Is there a way to do this? strtotime() did not work either.

It seems that all of the other readings about epoch time are about changing date to epochtime, but I'm looking to go the other way around. Any help is greatly appreciated.

Answer

user1701252 picture user1701252 · Nov 20, 2012

Fixed it using substr($epoch, 0, 10) and then used the date function for anyone wondering about the 13 digit epoch times.

Here is a sample code:

echo date("Y-m-d H:i:s", substr("1477020641000", 0, 10));
// Result: 2016-10-20 20:30:41