All In One Script



PHP,HTLM,CSS,Jquery,AJAX,Javascript and etc doubts and sample codes

  • Home
  • Javascript
  • PHP
  • CSS
  • SQL/MYSQL

Determine a User's Timezone

by Blogger 2:47:00 AM html Javascript timezone timezone-offset

Determine a User's Timezone

Is there any standard way for a Web Server to be able to determine a user's timezone within a web page? Perhaps from a HTTP header or part of the user-agent string?

Answer:

timezone.js:

function ajaxpage() {
    var url = "timezone.php";
    var visitortime = new Date();
    var time = visitortime.getTimezoneOffset()/60;
    var page_request = false;

    if (window.XMLHttpRequest) {
        page_request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { 
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try{
                page_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
            }
        }
    }
    else {
        return false;
    }

    page_request.onreadystatechange = function() {
            loadpage(page_request, containerid);
    }

    if (bustcachevar) {
        bustcacheparameter=(url.indexOf("?")!=-1) ? "&"+new Date().getTime() : "?"+new Date().getTime();
    }

    page_request.open('GET', url+bustcacheparameter+"&time="+time, true);
    page_request.send(null);
}

function loadpage(page_request, containerid) {
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
        document.write('<meta http-equiv="refresh" content="0;url=http://example.com/"/>');
    }
}

timezone.php:

<?php
session_start();
$_SESSION['time'] = $_GET['time'];
?>

When you want to use it add onLoad="ajaxpage(); to the body tag and it should cause the timezone to be stored in the PHP session variable $_SESSION['time']
Edit: P.S. This is untested.
source


READ MORE
SHARE :

Search This Blog

Followers

  • Popular
  • Recent
  • Comments
    How to efficiently iterate over each Entry in a Map?
    How to avoid Java code in JSP files?
    Why does this code using random strings print “hello world”?
    Serving XHTML and self-closing tags
    Get first key in a (possibly) associative array?
    Does finally always execute in Java?
    In Java, difference between default, public, protected, and private
    Length of a JavaScript object
    Solved : curl_init() function not working in Ubuntu
    How can I make the cursor a hand when a user hovers over a list item?

Instagram

About

Popular Posts

  • How to efficiently iterate over each Entry in a Map?
    How to efficiently iterate over each Entry in a Map? If I have an object implementing the  Map  interface in Java and I wish to iterate...
  • How to avoid Java code in JSP files?
    How to avoid Java code in JSP files? I'm new to Java EE and I know that something like the following three lines <%= x + 1 %...
  • Why does this code using random strings print “hello world”?
    Why does this code using random strings print “hello world”? The following print statement would print "hello world". Could a...
  • Serving XHTML and self-closing tags
    Serving XHTML and self-closing tags I am trying to follow the xhtml 1.0 strict standard as I am creating my website. Right now, validat...
  • Get first key in a (possibly) associative array?
    Get first key in a (possibly) associative array? What's the best way to determine the first key in a possibly associative array? My...
  • Does finally always execute in Java?
    Does finally always execute in Java? I have a try/catch block with  return s inside it. Will the finally block be called? For example...
  • In Java, difference between default, public, protected, and private
    In Java, difference between default, public, protected, and private In Java , are there clear rules on when to use each of access modifi...
  • Length of a JavaScript object
    Length of a JavaScript object If I have a JavaScript object, say var myObject = new Object (); myObject [ "firstname" ] ...
  • Solved : curl_init() function not working in Ubuntu
    Solved : curl_init() function not working in Ubuntu  Here solved the error  Fatal error: Call to undefined function curl_init() ...
  • How can I make the cursor a hand when a user hovers over a list item?
    How can I make the cursor a hand when a user hovers over a list item? I've got a list, and I have a click handler for its items: ...

statcounter



statcounter



Template Created By ThemeXpose & Blogger Templates