Is there a standard way for a web server to determine what time zone offset a user is in?
From an HTTP header or part of the user-agent description, perhaps?
views:
8601answers:
13I would think that you could get an approximate idea from their IP address. Anyone know of any services that will match IP to geographic location, or a periodically updated database?
There are no HTTP headers that will report the clients timezone so far although it has been suggested to include it in the HTTP specification.
If it was me, I would probably try to fetch the timezone using clientside JavaScript and then submit it to the server using Ajax or something.
Javascript is the easiest way to get the client's local time. I would suggest using an XMLHttpRequest to send back the local time, and if that fails, fall back to the timezone detected based on their IP address.
As far as geolocation, I've used MaxMind GeoIP on several projects and it works well, though I'm not sure if they provide timezone data. It's a service you pay for and they provide monthly updates to your database. They provide wrappers in several web languages.
timezone.js:
function ajaxpage(){
var url = "timezone.php";
var visitortime = new Date();
vat 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.
The most popular (==standard?) way of determining the time zone I've seen around is simply asking the user ximself. If your website requires subscription, this could be saved in the users' profile data. For anon users, the dates could be displayed as UTC or GMT or some such.
I'm not trying to be a smart-alec. It's just that sometimes some problems have finer solutions outside of any programming context.
The magic all seems to be in
visitortime.getTimezoneOffset()
That's cool, I didn't know about that. Does it work in IE, etc? From there you should be able to use JS to ajax, set cookies, whatever. I'd probably go the cookie route myself.
You'll need to allow the user to change it though. We tried to use geolocation (via maxmind) to do this a while ago, and it was wrong reasonably often - enough to make it not worth doing, so we just let the user set it in their profile, and show a notice to users who haven't set theirs yet.
Anyone know of any services that will match IP to geographic location
Well, lucky for you that answer can be found on our very own stackoverflow website: http://stackoverflow.com/questions/1033/ip-to-country
spoiler: http://www.hostip.info/use.html
If you happen to be using OpenID for authentication, Simple Registration Extension would solve the problem for authenticated users (You'll need to convert from tz to numeric).
Another option would be to infer the time zone from the user agent's country preference. This is a somewhat crude method (won't work for en-US), but makes a good approximation.
Ask the user.
If you get the time zone from the user's computer, and it is set wrong, then what?
Here is an article (with source code) that explains how to determine and use localized time in an ASP.NET (VB.NET, C#) application:
In short, the described approach relies on the JavaScript getTimezoneOffset function, which returns the value that is saved in the session cookie and used by code-behind to adjust time values between GMT and local time. The nice thing is that the user does not need to specify the time zone (the code does it automatically). There is more involved (this is why I link to the article), but provided code makes it really easy to use. I suspect that you can convert the logic to PHP and other languages (as long as you understand ASP.NET).
With PHP date function you will get the date time of server on which site is located. The only way to get user time is to use JavaScript.
But I suggest you to, if your site have registration required then best way is to ask user while registration as compulsory field. You can list various time zones in register page and save that in database. After this if user login to site then you can set default time zone for that session as per users’ selected time zone. You can set any specific time zone using PHP function date_default_timezone_set. This set the specified time zone for users.
Basically users’ time zone is goes to client side, so we must use JavaScript for this.
Below is the script to get users’ time zone using PHP and JavaScript.
<?php
#http://www.php.net/manual/en/timezones.php List of Time Zones
function showclienttime()
{
if(!isset($_COOKIE['GMT_bias']))
{
?>
<script type="text/javascript">
var Cookies = {};
Cookies.create = function (name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else {
var expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
this[name] = value;
}
var now = new Date();
Cookies.create("GMT_bias",now.getTimezoneOffset(),1);
window.location = "<?php echo $_SERVER['PHP_SELF'];?>";
</script>
<?php
} else {
$fct_clientbias = $_COOKIE['GMT_bias'];
}
$fct_servertimedata = gettimeofday();
$fct_servertime = $fct_servertimedata['sec'];
$fct_serverbias = $fct_servertimedata['minuteswest'];
$fct_totalbias = $fct_serverbias – $fct_clientbias;
$fct_totalbias = $fct_totalbias * 60;
$fct_clienttimestamp = $fct_servertime + $fct_totalbias;
$fct_time = time();
$fct_year = strftime("%Y", $fct_clienttimestamp);
$fct_month = strftime("%B", $fct_clienttimestamp);
$fct_day = strftime("%d", $fct_clienttimestamp);
$fct_hour = strftime("%I", $fct_clienttimestamp);
$fct_minute = strftime("%M", $fct_clienttimestamp);
$fct_second = strftime("%S", $fct_clienttimestamp);
$fct_am_pm = strftime("%p", $fct_clienttimestamp);
echo $fct_day.", ".$fct_month." ".$fct_year." ( ".$fct_hour.":".$fct_minute.":".$fct_second." ".$fct_am_pm." )";
}
showclienttime();
?>
But as per my point of view, it’s better to ask to the users if registration is mandatory in your project.