LeviZoesch
LeviZoesch

Reputation: 1621

Convert Unix Timestamp UTC 0 to Readable Date/Time

I have a unix timestamp that is saved at UTC 0. I need to convert it to GMT -08:00 ( PST - America LA ).

I need to take that timestamp of 1546329808471 and convert it into

Date format - m/d/Y

Time Format h:i a

I have

$time = new DateTime(date('h:i a', $pickup['timestamp']/1000)); // which returns the time in UTC 0.
$date = date('m/d/Y', $pickup['timestamp'] / 1000); // is the date in UTC 0.

returning

01/01/2019 at 08:03 am which is correct for UTC 0, but not PST.

Upvotes: 1

Views: 302

Answers (2)

Ankur Tiwari
Ankur Tiwari

Reputation: 2782

You can simply use setTimezone() and create an object of DateTimeZone class to achieve your result.

$pst = new DateTimeZone('America/Los_Angeles');
$time = new DateTime(date('m/d/Y h:i a', $pickup['timestamp']/1000)); 
$time->setTimezone($pst);
print_r($time);

You can read more about it from here

Upvotes: 1

nottherealironman
nottherealironman

Reputation: 389

You should set the default timezone first using date_default_timezone_set and then display the time/date as below:

date_default_timezone_set('America/Los_Angeles');
$time = new DateTime(date('h:i a', $pickup['timestamp']/1000)); 
$date = date('m/d/Y', $pickup['timestamp'] / 1000);

Upvotes: 0

Related Questions