Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect user's timezone?

I need to know what time zone is currently my users are in based on their IP or http header.

I got many answer regarding this issue, but i could not understood those answer. Some said use -new Date().getTimezoneOffset()/60 (from here). But what does it mean?

I have a date_default_timezone_set("Asia/Calcutta"); in the root of my (index.php) page. So for this I have to get the timezone dynamically and set it in place of Asia/Calcutta.

like image 250
Dan Avatar asked May 13 '13 15:05

Dan


People also ask

How do I know what timezone a user is in?

getTimezoneOffset()/60; The method getTimezoneOffset() will subtract your time from GMT and return the number of minutes. So if you live in GMT-8, it will return 480. To put this into hours, divide by 60.

How do I find the timezone of my server?

Checking Your Current TimezoneThe currently configured timezone is set in the /etc/timezone file. To view your current timezone you can cat the file's contents. Another method is to use the date command. By giving it the argument +%Z , you can output your system's current time zone name.

How do I get client time zone in laravel?

It works by listening for the user login event and setting the timezone in the database. It uses Laravel GeoIP to look up the user using an IP address. You can also convert the localized date back to UTC: 1Timezone::convertFromLocal($request->get('publish_at'));


2 Answers

To summarize Matt Johnson's answer in terms of code:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"> </script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js"> </script> <script type="text/javascript">   $(document).ready(function(){     var tz = jstz.determine(); // Determines the time zone of the browser client     var timezone = tz.name(); //For e.g.:"Asia/Kolkata" for the Indian Time.     $.post("url-to-function-that-handles-time-zone", {tz: timezone}, function(data) {        //Preocess the timezone in the controller function and get        //the confirmation value here. On success, refresh the page.      });   }); </script> 
like image 188
Elavarasan Muthuvalavan - Lee Avatar answered Sep 29 '22 13:09

Elavarasan Muthuvalavan - Lee


Time zone information of the browser is not part of the HTTP spec, so you can't just get it from a header.

If you have location coordinates (from a mobile device GPS, for example), then you can find the time zone using one of these methods. However, geolocation by IP address is not a great solution because often the IP is that of an ISP or proxy server which may be in another time zone.

There are some strategies you can use to try to detect the time zone, such as using jsTimeZoneDetect library, which is a great starting point, but imperfect enough that you can't just rely on that alone. If you're using moment.js, there's a built in function in moment-timezone called moment.tz.guess() that does the same thing.

The idea of using JavaScript's getTimezoneOffset() function is flawed in that you are not getting a time zone - just a single offset for a particular date. See the TimeZone tag wiki's section titled "TimeZone != Offset".

However you look at it, ultimately you have to decide on one of two approaches:

  • Ask the user to tell you their time zone, from some sort of drop-down list or map-based timezone picker control.

OR

  • Only send time to the browser in UTC, and use JavaScript on the browser to convert to whatever local time zone the user might have their computer set to.

I discuss this in more detail (from a c# perspective) in this answer.

like image 44
Matt Johnson-Pint Avatar answered Sep 29 '22 12:09

Matt Johnson-Pint