tags:

views:

238

answers:

2

Hi,

Is there a way to find out visitors' timezone in PHP?

+2  A: 

You can't do it alone with PHP as that information is not available. However, if you know the user's location you may be able to do some geo-locating and determine a probably timezone.

You need javascript's help. I googled the following JS snippet which returns the client's timezone offset:

var tzo=(new Date().gettimezoneoffset()/60)*(-1);

From here, you can set a cookie with JS which is accessible with PHP. Alternativly, you could grab this information with an ajax call.

Mike B
Setting cookie and access it with PHP is great idea. Thanks!
Moon
What if user using his laptop abroad?
Nazariy
@Nazariy The timezone shown to the user should reflect what is set in their OS. If they are abroad and want to see timezones relative to that, they should set it as such in the OS. As always, timezone information should be present with every displayed date on your site to avoid confusion.
Mike B
I agree with Mike, using geo location takes away the users power to determine what timezone they are in
Most of the people are not that geeky as we are, and they would not bother to mess with system settings. For other reason cookie might be restricted or javascript fail.For sure they need an option to switch time zone.
Nazariy
+2  A: 

You can match user IP with GeoIP database end extract his region. Also check this API http://www.ipinfodb.com/ip_location_api.php

Nazariy
@Nazairy // wow..that is a great website. It is free as well. Thanks!!
Moon
What if an IP range crosses over a timezone?
Mike B
I did Google for this issue and found nothing except our conversation.I presume that by overlapping you mean converting IP to Longitude which would give us fuzzy result. With GeoIP database IP address linked with city, which belongs to specific time zone.
Nazariy