简体   繁体   English

如何允许用户更改时区,但停留在同一页面上

[英]How to allow a user to change timezone, but stay on the same page

Basically, I need the user to be able to change the timezone, but keep them on the same page. 基本上,我需要用户能够更改时区,但要保持它们在同一页面上。 The reason for this is, I'm using a jQuery/ajax page loader script, and when I use the form/php I currently have it just goes to the actual php file (the php file works fine). 这样做的原因是,我使用的是jQuery / ajax页面加载器脚本,当我使用form / php时,我目前拥有的只是实际的php文件(php文件工作正常)。 I just need it to stay on the page. 我只需要它留在页面上。

Here is the code for the page: 这是页面的代码:

<?php
date_default_timezone_set($_POST['timezones']);
?>

<form action="data4.php" method="post">
<select name="timezones">
  <option>Select your timezone</option>
  <option value="Europe/London">Europe/London</option>
  <option value="America/Mexico_City">Central</option>
  <option value="America/New_York">Eastern</option>
</select>
<input type="submit" value="Submit" />
</form>

I do plan on adding more timezones, this was just for demonstration. 我确实计划添加更多时区,这只是为了演示。

If anyone has some useful information/help, I'd be extremely grateful. 如果有人有有用的信息/帮助,我将非常感谢。

One way is that - you can fire an ajax request, when changes the timezone, from backend timezone will be changed. 一种方式是-您可以触发ajax请求,当更改时区时,后端时区将被更改。 But if need to change somewhere time in the same page then need to refresh that section as well with ajax. 但是,如果需要在同一页面的某处更改时间,则还需要使用Ajax刷新该部分。

add one class in dropdown, name is change_time_zone_trigger 在下拉列表中添加一个类,名称为change_time_zone_trigger

<select name="timezones" class="change_time_zone_trigger">
<option>Select your timezone</option>
<option value="Europe/London">Europe/London</option>
<option value="America/Mexico_City">Central</option>
<option value="America/New_York">Eastern</option>
</select>

Then create a trigger when evner clicks on any timezone. 然后在evner单击任何时区时创建一个触发器。

$(document).ready(function() {
  $('body').delegate('.change_time_zone_trigger', 'change', function(evt) {
    // here put your ajax code that will hit the server when ever any timezone will be changed.

  });

});

Here is one method. 这是一种方法。

<!doctype html>
<html>
    <head>
<?php
        session_start();
        if(!isset($_SESSION['timezone'])) {
            if(!isset($_REQUEST['offset'])) {
?>

        <script type="text/javascript">
            var d = new Date()
            var offset= -d.getTimezoneOffset()/60;
            location.href = "<?php echo $_SERVER['PHP_SELF']; ?>?offset="+offset;
        </script>
<?php
            } else {
                $zonelist = array('Kwajalein' => -12.00, 'Pacific/Midway' => -11.00, 'Pacific/Honolulu' => -10.00, 'America/Anchorage' => -9.00, 'America/Los_Angeles' => -8.00, 'America/Denver' => -7.00, 'America/Tegucigalpa' => -6.00, 'America/New_York' => -5.00, 'America/Caracas' => -4.30, 'America/Halifax' => -4.00, 'America/St_Johns' => -3.30, 'America/Argentina/Buenos_Aires' => -3.00, 'America/Sao_Paulo' => -3.00, 'Atlantic/South_Georgia' => -2.00, 'Atlantic/Azores' => -1.00, 'Europe/Dublin' => 0, 'Europe/Belgrade' => 1.00, 'Europe/Minsk' => 2.00, 'Asia/Kuwait' => 3.00, 'Asia/Tehran' => 3.30, 'Asia/Muscat' => 4.00, 'Asia/Yekaterinburg' => 5.00, 'Asia/Kolkata' => 5.30, 'Asia/Katmandu' => 5.45, 'Asia/Dhaka' => 6.00, 'Asia/Rangoon' => 6.30, 'Asia/Krasnoyarsk' => 7.00, 'Asia/Brunei' => 8.00, 'Asia/Seoul' => 9.00, 'Australia/Darwin' => 9.30, 'Australia/Canberra' => 10.00, 'Asia/Magadan' => 11.00, 'Pacific/Fiji' => 12.00, 'Pacific/Tongatapu' => 13.00);
                $index = array_keys($zonelist, $_REQUEST['offset']);
                $_SESSION['timezone'] = $index[0];
            }
        }
        date_default_timezone_set($_SESSION['timezone']);
?>

    </head>
    <body>
        Current Time Zone is <?php echo $_SESSION['timezone']; ?>
    </body>
</html>

Basically how this works is pretty simple, you can have this script be the very first thing your page does because the timezone will stay in the session. 基本上,它的工作方式非常简单,您可以让此脚本成为页面的第一件事,因为时区将保留在会话中。

Starting with the session_start(); 从session_start()开始; it will first check to see if there is a timezone variable in the SESSION, if not then it will check the REQUEST for and offset variable. 它将首先检查SESSION中是否存在时区变量,如果没有,则将检查REQUEST for和offset变量。 If not, the it will use the JavaScriptand reloads the page with the correct timezone that it detects from the browser by using a $_GET method checks the offset (which will return -12 through +12) and match against the array. 如果不是,它将使用JavaScript,并使用$ _GET方法检查偏移量(将返回-12到+12),并使用从浏览器检测到的正确时区重新加载页面,并与数组匹配。 Finally it will set the default date_time_zone to the detected JavaScript and your all set. 最后,它将默认的date_time_zone设置为检测到的JavaScript和所有设置。 You can have it display the time zone. 您可以让它显示时区。

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM