Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Epoch Time to Date PHP

Tags:

php

time

epoch

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.

like image 756
user1701252 Avatar asked Nov 20 '12 16:11

user1701252


People also ask

How do I convert epoch to date?

Because our Epoch time is specified in milliseconds, we may convert it to seconds. To convert milliseconds to seconds, first, divide the millisecond count by 1000. Later, we use DATEADD() to add the number of seconds since the epoch, which is January 1, 1970 and cast the result to retrieve the date since the epoch.

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.

What is epoch timestamp in PHP?

Definition and Usage. The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Why is epoch time 1970?

January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch. Early Unix engineers picked that date arbitrarily because they needed to set a uniform date for the start of time, and New Year's Day, 1970, seemed most convenient.


2 Answers

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 
like image 196
user1701252 Avatar answered Oct 12 '22 05:10

user1701252


There are a couple different ways you can do this.

First off, what you've got there is a Unix Epoch time. (1/1/1970), that makes everything MUCH easier.

In PHP, try

$epoch = 1344988800; $dt = new DateTime("@$epoch"); echo $dt->format('Y-m-d H:i:s'); 

To display your date

OR

If you want the long RFC232 date:

echo date('r', $epoch); 
like image 37
Jon Avatar answered Oct 12 '22 07:10

Jon