3

I'm trying to convert an epoch timestamp with php 5.3 with the following statement

date('d-m-Y',strtotime('1349042399999'));

to human readable format and getting wrong result: 01-01-1970what should return30-09-2012. I have been searching around and founding the following topic PHP strtotime returns a 1970 date when date column is null but did not help on my case.

Community
  • 1
  • 1
Kakuki Peter
  • 45
  • 1
  • 3

3 Answers3

7

The reason for that is that there are milliseconds embedded in that timestamp, which causes it to go over the integer overflow limit.

chop the last 3 characters, and you're good to go:

$original_timestamp = "1349042399999";
$timestamp = (int) substr($original_timestamp,0,-3);

echo date('d-m-Y',$timestamp);
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • @KakukiPeter: If this answer solved your problem, consider upvoting and accepting it, by clicking on the large green tick mark under the answer's score. – Madara's Ghost Sep 08 '12 at 20:56
  • Was working on this for hours, until I came across the suggestion to chop off the last 3 chars. Worked great! – blakeage Jan 14 '15 at 02:32
0

By using strtotime, you are trying to convert a timestamp into another timestamp. Your timestamp is also in microseconds. The date function just needs the timestamp without the microseconds like so:

date('d-m-Y',substr('1349042399999', 0, -3));
Kermit
  • 33,827
  • 13
  • 85
  • 121
0

I believe that the Second Rikudo solution worked because of the cast to int, not because of trimming the milliseconds.

It seems to me that this should work for you (it did for me)

$original_timestamp = "1349042399999"; date('d-m-Y', (int) $original_timestamp);