如何使用jQuery获取用户时区? [英] How to get user timezone using jquery?

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

问题描述

我想使用诸如'Asia/Calcutta''Australia/Darwin'

我在myserver上安装了php,其时间设置为'UTC'

I have php installed on myserver and its time set to 'UTC'

推荐答案

这是我在这里的第一篇文章,但希望我能对某人有所帮助.我被迫这样做是因为这个网站对我有很大帮助.谢谢stackoverflow成员!

This is my first post here but hopefully I can help someone. I felt compelled because this site has helped me a lot. Thanks stackoverflow members!

http://www.pageloom.com/automatic-timezone-detection- with-javascript

据我了解,此JavaScript代码非常准确,可以返回UST的偏移量(对应的Olson数据库时区名称),并可以处理夏令时(例如,美国-5:00,纽约,是真的).​​

From what I understand about this JavaScript code it is very accurate and will be able to return the offset from UST, the corresponding Olson Database timezone name, and handle the daylight savings time issue (ex. -5:00, America/New_york, true).

http://www.php.net/manual/en/timezones. America.php

上面的链接只是php支持的时区参数的列表,据我所知,与该JavaScript代码保持一致(我确信这样做是有益的,只是我不知道为什么我仍在编写我的代码而且我是php/mysql的新手.

The above link is just a list of the php supported timezone parameters, which as far as I can tell align with this JavaScript code (I am sure that is beneficial I just don't know why yet I am still writing my code and I am kinda new to php/mysql).

在HTML页面上运行此代码后,您将面临的唯一障碍可能是将这些值传递给php,然后将其传递给mysql(如果需要).我通过使用JQuery将这些值作为$ .post发送来实现了.我认为这是最简单的方法(如果我错了,请有人纠正我).我相信其他替代方法是使用AJAX命令(更复杂)或cookie(我认为吗?).

The only hurdle you will face after getting this code working on your html page will likely be getting these values to php and then to mysql if that is what you need. I achieved this by sending these values as a $.post using JQuery. I think this is the easiest way to do it (please someone correct me if I am wrong). I believe the other alternatives are using an AJAX command (more complicated) or cookies(I think?).

从Java脚本代码到服务器(PHP)的值之后,我将它们存储为会话变量,因此,如果我网站上的用户从不同于通常的时区登录,则它们将更改.但是,它们也可以很容易地保存到数据库中.

After I got the values from the Java-script code to the server (PHP) I stored them as session variables so they would change if the user on my site logged in from a different timezone then usual. However they could easily be saved to the database as well.

如果您使用JQuery路由,则此路径必须在执行$ .post的html文档的标头中.

If you use the JQuery route this needs to be in the header of the html document where the $.post is being executed.

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

可以在我发布的第一个URL上找到最新的timezonedetect脚本链接.

The most updated timezonedetect script can be found linked on that first url I posted.

//replace this comment with the most updated timezonedetect code from that first link
var timezone = jstz.determine_timezone();
var tzoffset = timezone.offset();
var tzname = timezone.name();
var tzdst = timezone.dst();
$.post("scripts/tzdetect.php", { tzoffset: tzoffset, tzname: tzname, tzdst: tzdst } );

如果要将数据存储为会话变量,则本例中的接收文件tzdetect.php应该看起来像这样.

The receiving file in this case tzdetect.php should look like this if you want the data stored as session variables.

<?php
session_start();
$_SESSION['tzname'] = $_POST['tzname'];
$_SESSION['tzoffset'] = $_POST['tzoffset'];
$_SESSION['tzdst'] = $_POST['tzdst'];
?>

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

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