简体   繁体   English

从unix时间戳返回本地时间与php中的时区标识符

[英]Return local time from unix timestamp with timezone identifier in php

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). 我在网页上有一个下拉框,它将时区标识符作为值(但是我可以更改它以保存GMT偏移,如果这是更好的选择)。 Currently a user would be saving something like "America/Denver" as his / her time zone. 目前,用户将节省诸如“America / Denver”之类的东西作为他/她的时区。

I need to convert a unix timestamp into the user's local time for display in another portion of the page. 我需要将unix时间戳转换为用户的本地时间,以便在页面的另一部分中显示。 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? 将GMT偏移量存储在数据库中并将其用于时间计算会更好吗? Can someone please point me in the right direction here? 有人可以指点我在正确的方向吗?

Thanks! 谢谢!

The easiest approach would be to use DateTime with: 最简单的方法是使用DateTime

$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. 可以随意更改时区以使格式化功能重新计算本地时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM