18

hi i am creating a web application , where if a user register we will show the created date.

For that we are using Current time stamp in my sql table.Which shows server Time.But we don't know how to convert Time according to user time zone.

Because we are not getting user's country .

Can any on help me to fix it

Thanks in advance :)

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Well, how do you *want* to fix it? The most reliable option may be asking a user for their time zone – Pekka Mar 05 '11 at 11:03
  • is there any other option like detecting time zone automatically – Balaji Chandrasekaran Mar 05 '11 at 11:04
  • See this question: [Automatically detect user's current local time with JavaScript or PHP](http://stackoverflow.com/questions/863474/automatically-detect-users-current-local-time-with-javascript-or-php) – Pekka Mar 05 '11 at 11:05

5 Answers5

24

Use javascript solution

http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/

Demo should show your timezone in select box.

http://onlineaspect.com/examples/timezone/index.html (Dead link)

0x9BD0
  • 1,542
  • 18
  • 41
supersuphot
  • 543
  • 4
  • 8
  • 1
    I followed a ton of links which took me to https://bitbucket.org/pellepim/jstimezonedetect - which seems a much more robust solution than your link, which guessed close, but didn't realise I was in daylight savings. – Bron Gondwana Jan 17 '14 at 13:12
8

You can't get a user's timezone on the server side, and you can only make a guess at it on the client side.

If you rely on getting the user's IP address then you could geolocate that and deduce a time.

The way this is usually done is by asking the user (when they register, for instance) what timezone they are in and then use this in your time calculations.

cusimar9
  • 5,185
  • 4
  • 24
  • 30
  • 8
    This also provides an opportune question with which to catch spambots by including two fake timezone choices since bots will often choose the first or last item in a dropdown list. – JustinStolle Mar 05 '11 at 11:32
  • You could have the webpage submit the current time in all requests the user sends (with javascript). Then in php it could figure out the offset by comparing the received time from the request with the time that php believes it is. – Tom Jenkinson Aug 29 '13 at 15:55
  • I prefer timezone registration. Nowadays people often travel and sometimes they want to know some event in their country timezone, not the timezone of their current IP or browser. This means that in the browser you should also be careful with Date() objects because they automagically shift to browser's timezone when stringifying, which might be unexpected. – JustAMartin Oct 04 '17 at 12:17
5

For geolocation you can use http://ipinfodb.com/, work great with an API!

function GeoLocation($precision='city',$ip,$api) {

    if($precision == "country") {
        $file = "ip_query_country.php";
    }
    else {
        $file = "ip_query.php";
    }

    $params = @file_get_contents("http://api.ipinfodb.com/v2/ip_query.php?key=".$api."&ip=".$ip."&timezone=true");
    $fields = @new SimpleXMLElement($params);
    foreach($fields as $field => $val) {
        $result[(string)$field] = (string)$val;
    }
    return $result;
}
Robin
  • 443
  • 5
  • 18
  • 10
    If you do decide to do this then please allow it to be easily overriden by a user. At work I'm in the UK but our internet access is connected via New York and there are quite a lot of websites that wrongly deduce where I am and have no way to correct it. – jcoder Mar 05 '11 at 12:27
  • @jcoder the issue with that is it allows any user to change their timezone off the back. Sounds like a VPN to me as users can now enter a fake timezone should they decide to – Rotimi Feb 04 '20 at 11:29
2

With a small "cheating" (using client side scripting) and posting the result to your server, you can access your client's timezone.

For example you can use this javascript library:

https://bitbucket.org/pellepim/jstimezonedetect

var tz = jstz.determine(); // Determines the time zone of the browser client
tz.name(); // Returns the name of the time zone eg "Europe/Berlin"

Demo: http://pellepim.bitbucket.org/jstz/

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
1

The most reliable method is to ask the user to set his own timezone. I think it's a good idea to have a timestamp of when the users account was created in your own database for your own use.

These are the steps I would take to have a users timezone:

1- Preserve the timestamp column that you have for your own usage
2- Add a new column for the actual users timezone
3- Use javascript to retrieve the timezone:
new Date().getTimezoneOffset()/60;<br/>
4- Also allow users to set there own timezone as well

You can also take it a step further and use a geo-location service to detect a users timezone.

Philoxopher
  • 1,660
  • 3
  • 15
  • 17