简体   繁体   中英

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). 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. But if need to change somewhere time in the same page then need to refresh that section as well with ajax.

add one class in dropdown, name is 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.

$(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(); 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. 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. Finally it will set the default date_time_zone to the detected JavaScript and your all set. You can have it display the time zone.

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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