Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

datetime - 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). 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...