Kevin Dente Kevin Dente - 7 months ago 30
HTML Question

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.

Comments