开发者

How to display all datetime on website in user's GMT locale?

开发者 https://www.devze.com 2023-01-11 04:49 出处:网络
I store all datetime values as unix timestamp. Registered users can set personal GMT locale in their profile. What should I do to display all datetime开发者_Go百科 on website in user\'s GMT locale?

I store all datetime values as unix timestamp. Registered users can set personal GMT locale in their profile. What should I do to display all datetime开发者_Go百科 on website in user's GMT locale?

Thank you


There's no such thing as "user's GMT locale". You must be referring to the user's timezone.

You can convert unix timestamps to dates in the user timezone this way:

$timestamp = ...;
$tz = new DateTimezone("Europe/Lisbon"); //substitute by the user's timezone
$d = new DateTime("@$timestamp");
$d->setTimezone($d);
echo $d->format(DateTime::RFC822);

If you only have a GMT offset, you can use:

$tz = new DateTimezone("Etc/GMT-12");

Note, however, that if you use GMT offsets, you will have to change them when the users enter or leave daylight saving time.


With PHP:

date("Y-m-d H:i:s", $timestamp);

With MySQL:

FROM_UNIXTIME()
0

精彩评论

暂无评论...
验证码 换一张
取 消