Return local time from unix timestamp with timezone identifier in php

对着背影说爱祢 提交于 2019-12-29 01:47:51

问题


I have a dropdown box on a webpage that takes a timezone identifier as the value (however I can change it to save the GMT offset if that is a better choice). Currently a user would be saving something like "America/Denver" as his / her time zone.

I need to convert a unix timestamp into the user's local time for display in another portion of the page. What is the best way to accomplish this?

Would it be better to store the GMT offset in the database and use that for a time calculation? Can someone please point me in the right direction here?

Thanks!


回答1:


The easiest approach would be to use DateTime with:

$t = new DateTime();
$t->setTimestamp( $time=time() );
$t->setTimeZone(new DateTimeZone("America/Denver"));
print $t->format(DateTime::RFC850);

The timezone can be changed at will to cause the format function to recalculate the local time.



来源:https://stackoverflow.com/questions/5397775/return-local-time-from-unix-timestamp-with-timezone-identifier-in-php

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