Auto-detect timezone of user on server-side

2,002 views
Skip to first unread message

Andrew Quan

unread,
Nov 12, 2013, 8:18:41 AM11/12/13
to f3-fra...@googlegroups.com

Hi guys,


So basically I would like to get the users timezone data via the Web\Geo plugin:

$timezone_data = \Web\Geo::instance()->tzinfo($timezone);


How do you auto-detect the users timezone in order to get the timezone data? Via IP?

bcosca

unread,
Nov 12, 2013, 11:16:32 AM11/12/13
to f3-fra...@googlegroups.com
Try to figure out a way to pass the results of this Javascript snippet to your server:

var T=new Date();
return T.getTimezoneOffset()/60;

Andrew Quan

unread,
Nov 12, 2013, 11:21:15 AM11/12/13
to f3-fra...@googlegroups.com

Aw, yeah, so I take it that there isn't a "pure server side" solution for this?

bcosca

unread,
Nov 12, 2013, 11:35:03 AM11/12/13
to f3-fra...@googlegroups.com
No, unless some request header was submitted. This is not in the HTTP specs.

ikkez

unread,
Nov 12, 2013, 11:39:27 AM11/12/13
to f3-fra...@googlegroups.com

bcosca

unread,
Nov 12, 2013, 12:27:57 PM11/12/13
to f3-fra...@googlegroups.com
Where will the arguments (country, region) come from? Server can't predict that if client doesn't send anything.

Andrew Quan

unread,
Nov 12, 2013, 12:39:42 PM11/12/13
to f3-fra...@googlegroups.com
I was able to do that using Web\Geo::instance()->location() but there are prerequisites for that geoip extension http://www.php.net/manual/en/geoip.requirements.php

So unless you got dedicated hosting, it probably won't be active by default with a shared host.

ikkez

unread,
Nov 12, 2013, 1:31:32 PM11/12/13
to f3-fra...@googlegroups.com
both parameters can be fetched by the IP adress. see the parameter description on the php.net docs page.

geoip_country_code_by_name() and geoip_region_by_name()

the GeoIP PECL extension is needed anyway to use the location method, so maybe you could add the timezone to the $out array??

Andrew Quan

unread,
Nov 12, 2013, 1:41:03 PM11/12/13
to f3-fra...@googlegroups.com
Oh I see, I must be getting the location from the 2nd half of the location function then:


                if (($req=$web->request('http://www.geoplugin.net/json.gp'.
                        ($public?('?ip='.$ip):''))) &&
                        $data=json_decode($req['body'],TRUE)) {
                        $out=array();
                        foreach ($data as $key=>$val)
                                if (!strpos($key,'currency') && $key!=='geoplugin_status'
                                        && $key!=='geoplugin_region')
                                        $out[$fw->snakecase(substr($key, 10))]=$val;
                        return $out;
                }

bcosca

unread,
Nov 12, 2013, 9:23:48 PM11/12/13
to f3-fra...@googlegroups.com
Mind you, the IP address is not a reliable way of getting the country, and subsequently the time zone. Some countries have overlapping IP address ranges.

Andrew Quan

unread,
Nov 13, 2013, 2:10:21 AM11/13/13
to f3-fra...@googlegroups.com
Ah, that sucks.

ikkez

unread,
Nov 13, 2013, 3:07:22 AM11/13/13
to f3-fra...@googlegroups.com
That's true. Best way is to have a selection of timezones in your user profile, where the user selects his favorite. But just to mention it here, it could be a good point to guess a timezone... for preselections or whatever ^^

Andrew Quan

unread,
Nov 13, 2013, 3:12:06 AM11/13/13
to f3-fra...@googlegroups.com

Yeah, that is the fallback, was just hoping to create a seamless experience.

stu warneski

unread,
Nov 14, 2013, 10:34:12 PM11/14/13
to f3-fra...@googlegroups.com
just be aware geoLite data is not as accurate as a paid subscription to their MaxMind service. 
ISPs change netblocks (new ip ranges assigned) and that can throw off your accuracy a lot. 
i get insulted all the time on the web when sites think i'm in a different part of the state.
but i don't get mad. i just eat their cookies.
Reply all
Reply to author
Forward
0 new messages