开发者

How to determine UTC offset from server timezone?

I've found many examples about UTC tables and php date methods to convert it, but I still miss a simple way after got server date, to converting it into an user timezone selection on my web page.

On this page http://vkham.com/UTC.html I've found a clear guide to understand the range, but I don't know how to connect for example "Europe/Rome" on the table, so there is something more clear about it?

I know the timezone of my server (America/Chicago) but I still don't know a way to change it from UTC method to a different timezone selected from the user machine (for example "Europe/Rome")

I tryied something, but I'still miss something, and i don't know what is it:

<?php
$timezone = date ("e");
$date = date ('Y-m-d H:i:s');
print $date." - this is my server date, and his timezone is - $timezone<br/>";

$user_timezone = "Europe/Rome"; // there is a way to convert Europe/Rome to UTC -13?
$selected_timezone = "-13"; // is -13 like Europe/Rome in all cases or only because my server is America/Chicago?
$date_user = date ("Y-m-d H:i:s $sele开发者_高级运维cted_timezone");
$str_date_user =  strtotime ($date_user);
$new_user_date = date ('Y-m-d H:i:s', $str_date_user);
print $new_user_date . " - this is my server date, and his timezone is - $user_timezone";
?>

Doesn't exist a way to convert Europe/Rome to -13 for UTC timezone?

Is -13 like Europe/Rome in all cases or only because my server is America/Chicago?


You can use gmdate to generate a date that is displaying the UTC time - whatever timezone your server is running in. Then you can simply add the timezone difference as hours * 3600 to the timestamp you use for generating the date to get the user's time.

A different way would be setting the local time temporary to the user's timezone by using date_default_timezone_set.

A simple example for the first idea would be the following:

<?php
$offset   = -13 * 3600; // timezone offset for UTC-13
$utcTime  = gmdate( 'd.m.Y H:i' );
$userTime = gmdate( 'd.m.Y H:i', time() + $offset );
?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜