使用Javascript/PHP获取用户时区字符串? [英] Get user timezone string with Javascript / PHP?

查看:85
本文介绍了使用Javascript/PHP获取用户时区字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在singup上获取用户的时区作为字符串.例如:

I'm trying to get the user's timezone as a string on singup. example:

$timezone = "Asia/Tel_Aviv";

在研究此问题时,我了解了如何使用Javascript获取时区偏移,但是我仍然不清楚如何将时区偏移量转换为php中的时区字符串,如上所示?

While researching the issue, I got how to Get timezone offset with Javascript, but I'm still unclear about how can I translate the timezone offset to a timezone string in php, as shown above?

或者,我在Javascript/PHP中使用了哪些其他CAS方法来获取每个用户的时区字符串?

Or, which other method cas I use in Javascript / PHP for getting the timezone string for each user?

我真的不确定如何解决这个问题.

I'm really not sure how to approach this.

推荐答案

您不能仅在PHP中执行此操作.

You can't do this in PHP alone.

您可以使用Javascript设置Cookie中的值,然后使用PHP在下一页(重新)加载中读取Cookie.

You can use Javascript to set the value in a cookie, then use PHP to read the cookie on the next page (re)load.

Javascript:

Javascript:

var dateVar = new Date()
var offset = dateVar.getTimezoneOffset();
document.cookie = "offset="+offset;

PHP:

echo $_COOKIE['offset'];

使用此选项将偏移量转换为PHP中的友好时区名称. Javascript返回以分钟为单位的偏移量,而此PHP函数期望输入以秒为单位-因此乘以60.第三个参数是您是否处于夏时制的布尔值.阅读手册并更新代码以适合您的需求.

Use this to convert the offset to the friendly timezone name in PHP. Javascript returns the offset in minutes, while this PHP function expects the input to be in seconds - so multiply by 60. The third parameter is a boolean value of whether or not you are in Daylight Savings Time. Read the manual and update the code to fit your needs.

echo timezone_name_from_abbr("", intval($_COOKIE['offset'])*60, 0);

http://php.net/manual/en /function.timezone-name-from-abbr.php

这篇关于使用Javascript/PHP获取用户时区字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆