49

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.

nickhar
  • 19,981
  • 12
  • 60
  • 73
user1701252
  • 1,501
  • 1
  • 11
  • 19

7 Answers7

73

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
Tarik
  • 4,270
  • 38
  • 35
user1701252
  • 1,501
  • 1
  • 11
  • 19
  • 2
    _Roughly_, epoch digit length decreases by 1 digit from 13 digits to 12 digits around 09/2001, then again on 03/1973, it will go negative and increase in length the further past 1970 you go. I would change this to `echo date("Y-m-d H:i:s", substr("1477020641000", 0, -3));` to account for this. – Josh Whitlow Nov 20 '19 at 17:04
37

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);
Jon
  • 1,055
  • 13
  • 24
  • See Updated. That should help. – Jon Nov 20 '12 at 16:47
  • regarding "makes everything MUCH easier": You should be aware that UNIX timestamps are a good and easy way to handle time, but not a rock-solid one. E.g. POSIX defines a day to have 86400 seconds which is not true when a leap second occurs. UNIX timestamps spread to two seconds then AFAIK :-) – mdo Nov 20 '12 at 16:57
  • Absolutely! However, I'm working in an environment where people create their own Epoch times, and don't always remember to write them down... so it's nice to see an Epoch with some community base :) – Jon Nov 20 '12 at 17:01
  • from PHP 5 there's also an ISO-8601 format that's compatible to the one expected by **Joda Time**'s `DateTimeFormatter`. This is particularly useful when sending a dateTime from PHP to a Java backend that automatically deserializes the string into a Joda Time's `DateTime`. Just use `'c'`: ```date('c', $epoch);``` – Clint Eastwood Apr 14 '16 at 18:42
  • hello @Jon!, surely do you want say **RFC 2822** (http://www.faqs.org/rfcs/rfc2822.html) / (https://www.php.net/manual/en/datetime.format.php (format **r**)) – VyR Oct 27 '22 at 18:50
15
$epoch = 1344988800;
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00 

More: http://www.epochconverter.com/programming/functions-php.php

twodayslate
  • 2,803
  • 3
  • 27
  • 43
  • 1
    No idea why, but the epoch times that it's calling is spitting out as this: 44858-07-07 12:50:35 When I print the epoch value it looks like this 1353430817435 It looks like the epoch is 13 characters? – user1701252 Nov 20 '12 at 16:47
  • 4
    what you have there is an UNIX timestamp plus three digit milliseconds, the value is the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. Skip the last three digits and feed it to date() – mdo Nov 20 '12 at 16:49
14
<?php
$epoch = '1353429562';

date_default_timezone_set('GMT');
echo date('Y-m-d H:i:s', $epoch);
Sphinxxx
  • 12,484
  • 4
  • 54
  • 84
Rawkode
  • 21,990
  • 5
  • 38
  • 45
2

FYI, API that send JSON data use epoch with millisecondes to be compatible with web browsers.

I don't know why PHP needs to remove the millisecondes, as it is the standard for every web browser.

So, you can use the left 10, or / 1000 (it's better to divide by 1000 if you don't want to get troubles on Nov 20, 2286!!!)

foxontherock
  • 1,776
  • 1
  • 13
  • 16
1

This works when you divide by 1000:

echo date("Y-m-d H:i:s", round(1353430853299/1000))

In this example, you will get 1353430853.299, so you must round it to get rid of the fractional part of the number. You could also use floor if you don't want it rounded.

Typewar
  • 825
  • 1
  • 13
  • 28
1

You need to select only 10 digits from epoch format

<?php echo date("Y-m-d H:i:s", substr("1621157545307", 0, 10)); ?>

output will be like :

2021-05-16 15:02:25

Manjeet Kumar Nai
  • 1,382
  • 1
  • 13
  • 16