3

So im trying to set the php timezone through javascript, so users automatically have the time set properly to their timezome. Now i found this code online, but unfortunately it transfers the determined value of the javascript through a GET value. Is there a way i can use this, but not require the inclusion of the GET value in the URL? as this happens to break my website due to how the .htaccess ReWriterules are set.

Here is the code for the timezone. What i need is a way to transfer the determined offset value from the javascript to the php without using the GET in the url. Is this possible?

<?php
session_start();
if(!isset($_SESSION['timezone']))
{
    if(!isset($_REQUEST['offset']))
    {
    ?>
    <script>
    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, $_GET['offset']);
        $_SESSION['timezone'] = $index[0];
    }
}
date_default_timezone_set($_SESSION['timezone']);

I have been told this may be possible with JSON, but after reading online im unable to understand the notation to properly use this.

3
  • If I understand correctly you're trying to get a piece of data to the server in an http request from the client side without using URL parameters. You could consider rewriting the page name (depends what your server allows; probably not a lot from what you've said), or another route to look at would be cookies. Commented Feb 25, 2012 at 21:22
  • yes that is what I am trying to do, rewriting the URL parameters results in 404s, which I can't have. Commented Feb 25, 2012 at 21:25
  • 1
    If the POST solution mentioned below won't work either, I would experiment with writing the data into a cookie client side (document.cookie="....=....") and reading the same data server side $_COOKIE['....'] Commented Feb 25, 2012 at 21:30

3 Answers 3

4

If you don't want to use a GET request, you could consider using a POST request instead. You can send the data from with an Ajax request in JavaScript. Using jQuery, this would look something like:

$.ajax({
   'url': 'PATH_TO_PHP_SCRIPT',
   'type': 'POST',
   'data': 'timezone=' + timezoneVariable
});
1
  • yeh. that'll do. but he wants the offset variable, not timezone... so send offset like timothy says and get it with offset = $_POST['offset'] Commented Feb 25, 2012 at 22:05
1

If you don't want to use JSON and ajax(XMLHttpRequest), you can use cookies. Since both javascript and php can read these, create one using javascript to store the timezone . Then in your php code read that cookie.

to create a cookie in javascript use :

document.cookie = 
    'myName=' + value + 
    '; expires=' + now.toGMTString() + 
    '; path=/';

and to read it in your php :

$_COOKIE['myName']
0

You can simply GET/POST an URL like /tz/TIME_ZONE where TIME_ZONE is whatever timezone you want to set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.