Hari
Hari

Reputation: 313

convert unix timestamp to date in Php

I already referred the questions in stackoverflow before posting this. In my case I need to convert 1426023505154 to date format.To makesure the time stamp is valid I checked in http://www.epochconverter.com. I used these codes to convert. But that doesn't worked:

date_default_timezone_set('Asia/Calcutta');
$date = new DateTime();
$date->setTimestamp(1426023505154);
echo $date->format('U = Y-m-d H:i:s') ;
echo gmdate("Y-m-d\TH:i:s\Z", 1426023505154);
echo echo date('d-m-Y', 1426023505154);

But all are resulting wrong results such as:

  1. 47158-11-20 21:49:14

  2. 20-11-47158

  3. 47158-11-20T16:19:14Z

Please let me know how to solve this. Thanks

Upvotes: 3

Views: 13958

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

echo date('Y-m-d H:i:s', 1426023505154 / 1000 );
// dividing by 1000 because the timestamp is in microseconds

Upvotes: 3

Martin
Martin

Reputation: 2427

The problem is your timestamp is in milliseconds and $date->setTimestamp uses seconds, you can fix this by dividing by 1000

$date = new DateTime();
$value = 1426023505154;
$date->setTimestamp($value/1000);

Upvotes: 8

Related Questions