Detecting Timezone By IP
Through Planet PHP I found an article on Pre-populating forms with the timezone . I'd normally add a comment instead, but the comment would almost be larger then the original post, so I am instead writing up an entry myself. The post describes several ways to obtain the user's timezone and use that to pre-fill a form. None of them are working properly though. I'll try to explain for each of them why not, but first of all it is important to know what a timezone actually is.
A timezone is a set of rules that determines the UTC offset for different times around the year for a specific location. Because of daylight savings time, a specific location can have two (or more) different UTC offsets depending on what point in time you look at it. Some areas might have had different rules in the past, even in the same country. An example here is China, where currently there is only one timezone, but previously there were multiple. Thus there are different timezones for the different locations in China, even though the current UTC offset is the same for all of them. Timezones are identified by Country/City or Country/Subcountry/City.
The offering by MaxMind allows you to link a country/region combination to Timezone identifier. For the US it subdivides this per state even. However, it misses the different timezones for Russia, Australia and even Indiana, USA.
IP2Location only provides a single UTC offset per IP range, totally ignoring daylight savings time.
The Hostip.info solution I can't access because phpclasses requires some stupid registration.
As for the author's own solution, using JavaScript, is flawed at least partly as well. In his example JavaScript only returns a UTC offset. Luckily it is possible to detect the correct timezone quite a bit better by using some of PHP's functionality. The following bit of code uses both the UTC offset and the timezone abbreviation to find out the user's timezone:
<?php
if (!isset($_GET['tzinfo'])) {
?>
<html>
<script type="text/javascript">
var d = new Date()
var tza = d.toLocaleString().split(" ").slice(-1)
var tzo = d.getTimezoneOffset()
window.location = window.location + '?tzinfo=' + tza + '|' + tzo
</script>
</html>
<?php
} else {
list( $abbr, $offset ) = explode( '|', $_GET['tzinfo']);
echo timezone_name_from_abbr( $abbr, $offset * -60 );
}
?>
This is still not perfect of course, because it would for example give the same result for most of Europe (Europe/Berlin).
Figuring out the user's timezone can be done by using a high-accuracy database of IPs to lat/longitudes such as hostip.info and MaxMind offer, as well as a mapping from location to timezone. The latter however, is not available as far as I know. If however a data file that has proper definitions of the different timezone boundaries exist (mostly, on a per-province level would be enough), then such a tool can be easily build. I saw that OpenStreetMap has such a map, but I can't really find the raw data for that unfortunately. It would however, be awesome to have such a data file.
Comments
http://www.geonames.org/ you can get the Olson timezones for any just about any city you can think of, or even just an longitude and latitude
@Avi: that is just brilliant. Even better is that you can download the file.
I commented last night but it doesn't seem to have shown up so here goes again . . .
The 'hostip.info' solution used a phpclasses script which used two web services to go from a location to a lat/long (Yahoo) and then from lat/long to a timezone (geonames). Maxmind could just as easily be used to provide the initial location though it would waste the code they've already created.
geonames is a really interesting service though it does need to be pointed out their service only gives the offsets at two times in the year. The dates when DST kicks in around the world are not the same universally so there will be days/weeks when some ambiguity exists.
I had some problems with your code. Firstly copying and pasting didn't seem to work. Everything seemed to end up on one line. Secondly, I get URLs like /rethans.php?tzinfo=10:57:34|-60. Looking at your code I think you were expecting a timezone abbreviation to be in there. I can find no examples online which show that behaviour for toLocaleString() though. What system/browser did you test this on?
I added a short fragment of this post to a comment on the original post. I think this is definitely worth a read and as no trackback was sent to my blog a comment seemed the best way to highlight that the conversation was continued here.
For simply just localizing dates, can just output all the dates in RFC1123 format, then parse() & toLocaleString() in javascript.
Your function takes timezone abbreviation as a parameter. But how do I obtain the timezone abbreviation?
There is the World Time Engine Local Time API that includes an 'IP to Local Time' call.
This sounds exactly like the problem you're having - determining a user's local time without using client-side scripting like Javascript.
I beleive they also have a free trial system where you can try the API for free too.
Life Line
Updated a pub
Fix gates
Created 2 gates
Created a waste_basket and a recycling
RE: https://mastodonapp.uk/@Silversnapples/116431673036774230
Please, do make sure to register if you have elections in your area.
New counter flow cycle path on York Bridge
Updated a crossing; Deleted 2 kerbs
Created a waste_basket
Green Woodpecker on the Ground
On one of my walks a few springs ago, I spotted this little chap on the ground moving around.
Spring Colours Along the Thames
It was aovely day for a walk. The spring colours are always so fresh.
Updated 2 benches
I walked 8.6km in 2h36m33s
Updated a bus_stop and a restaurant
I walked 2.0km in 20m06s
I've finished reading This Way Up. It's about maps, that went wrong.
It's a good read, but htyerr were several chapters that were written in a novel way (as a video transcript, a series of letters), and I found distracting from the a tail content. It'll have worked better in a produced video.
No mention of @openstreetmap though :-(
I walked 3.0km in 34m18s
I walked 1.1km in 10m21s
Updated a bench
I walked 4.9km in 56m05s
Created a tree; Updated 3 humps and a waste_basket
I walked 3.0km in 43m29s
The Early Cormorant Catches the Eel
Sorry, not the best photo! But I caught this Cormorant catching this large eel when looking for Bank Swallows, right next to Eel Pie Island in the Thames.
#Birds #BirdPhotography #BirdsOfMastodon #Photography #London
Updated an estate_agent office
I walked 2.2km in 37m51s
I went to my nieces' birthday party yesterday.
The theme was pink, and that included all the food, mostly died with beet root.
Shock and horror this morning when doing number two. Not only was my turd dark red, it was also glittering at me. Apparently the carrot cake had edible glitter...
So now I know what's worse than glitter.
😂 ✨ 💩 🟣





Shortlink
This article has a short URL available: https://drck.me/dtbi-6bk