Converting date from database to a different timez

2019-07-23 04:15发布

I have a website that allows people to post things. When they post, it gets inserted into the database and the database adds a timestamp.

When I hosted the database on my local machine, the timestamps were in my time zone. My new hosting website has it set to GMT time.

How can I make it display the time in my time zone? I've tried adding

date_default_timezone_set('America/New_York');

to my views just under the tag, but that doesn't help. Any ideas?

1条回答
ら.Afraid
2楼-- · 2019-07-23 04:40
// Set the time zone
$dateTimeZone = new DateTimeZone('America/New_York');

// Create the datetime and set the timestamp
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);

// Convert it
$dateTime->setTimeZone($dateTimeZone);

echo $dateTime->format('m/d/Y H:i:s');

Should work I guess

查看更多
登录 后发表回答