To get UTC offset in php I could use date("Z")
but as I think this will return the UTC offset relative to timezone of the server not relative to timezome of the user's computer, which I want to get. And when I searched I found that getting UTC offset relative to timezone of the user's computer can be done through the javascript getTimezoneOffset()
function which returns the time difference between UTC time and local time, in minutes. And I have successfully used this javascript function in my php script. Now my question is: Is the javascritp getTimezoneOffset()
function is always (or in most cases) reliable to return the accurate UTC offset relative to the user's computer?
javascript getTimezoneOffset()函数是否可靠返回准确的UTC偏移量?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dongyimo1293 2013-01-05 13:06关注
This will depend on the user's date/time settings. It will give you the offset based on what the browser sees the timezone is set on the user's computer.
But note that when you are using javascript, it is running on the client-side, i.e. on the browser, and not on the server-side. So you cannot use it in PHP, unless:
- You store it in a cookie (via javascript) the first time a user visits your website.
- You make an AJAX call to store a PHP code to store it in the session.
- If you want to display a time on the user's computer, you send the time difference between the date you want to show and
now()
, and then you add that to the user'sgetTime()
or you display the difference (i.e. 7 hours ago, 2 minutes ago, etc.).
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报