Converting date from database to a different timezone

泪湿孤枕 提交于 2019-12-24 03:51:13

问题


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:


// 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



来源:https://stackoverflow.com/questions/8114195/converting-date-from-database-to-a-different-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!