<p>Is there a standard way for a web server to be able to determine a user's timezone within a web page? </p>

<p>Perhaps from an HTTP header or part of the <code>user-agent</code> string?</p>

[{'answer_id': 29, 'author': 'Mads Kristiansen', 'author_id': 19, 'author_profile': 'https://Stackoverflow.com/users/19', 'pm_score': 8, 'selected': False, 'text': '<p>There are no HTTP headers that will report the clients timezone so far although it has been suggested to include it in the HTTP specification.</p>\n\n<p>If it was me, I would probably try to fetch the timezone using clientside JavaScript and then submit it to the server using Ajax or something.</p>\n'}, {'answer_id': 103, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 6, 'selected': False, 'text': '<p>JavaScript is the easiest way to get the client\'s local time. I would suggest using an <a href="http://en.wikipedia.org/wiki/XMLHttpRequest" rel="noreferrer">XMLHttpRequest</a> to send back the local time, and if that fails, fall back to the timezone detected based on their IP address.</p>\n\n<p>As far as geolocation, I\'ve used <a href="http://www.maxmind.com/app/ip-location" rel="noreferrer">MaxMind GeoIP</a> on several projects and it works well, though I\'m not sure if they provide timezone data. It\'s a service you pay for and they provide monthly updates to your database. They provide wrappers in several web languages.</p>\n'}, {'answer_id': 797, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 8, 'selected': False, 'text': "<p>The most popular (==standard?) way of determining the time zone I've seen around is simply <em>asking the users themselves.</em> If your website requires subscription, this could be saved in the users' profile data. For anon users, the dates could be displayed as UTC or GMT or some such.</p>\n\n<p>I'm not trying to be a smart aleck. It's just that sometimes some problems have finer solutions outside of any programming context.</p>\n"}, {'answer_id': 817, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 5, 'selected': False, 'text': "<p>All the magic seems to be in</p>\n<pre><code>visitortime.getTimezoneOffset()\n</code></pre>\n<p>That's cool, I didn't know about that. Does it work in Internet Explorer etc? From there you should be able to use JavaScript to Ajax, set cookies whatever. I'd probably go the cookie route myself.</p>\n<p>You'll need to allow the user to change it though. We tried to use geo-location (via <code>maxmind</code>) to do this a while ago, and it was wrong enough to make it not worth doing. So we just let the user set it in their profile, and show a notice to users who haven't set theirs yet.</p>\n"}, {'answer_id': 35819, 'author': 'Dmitry Shechtman', 'author_id': 3583, 'author_profile': 'https://Stackoverflow.com/users/3583', 'pm_score': 4, 'selected': False, 'text': '<p>If you happen to be using <a href="http://openid.net/" rel="noreferrer">OpenID</a> for authentication, <a href="http://openid.net/specs/openid-simple-registration-extension-1_0.html" rel="noreferrer">Simple Registration Extension</a> would solve the problem for authenticated users (You\'ll need to convert from tz to numeric).</p>\n\n<p>Another option would be to infer the time zone from the user agent\'s country preference. This is a somewhat crude method (won\'t work for en-US), but makes a good approximation.</p>\n'}, {'answer_id': 1809974, 'author': 'JD Isaacks', 'author_id': 46011, 'author_profile': 'https://Stackoverflow.com/users/46011', 'pm_score': 8, 'selected': False, 'text': '<pre><code>-new Date().getTimezoneOffset()/60;\n</code></pre>\n\n<p>The method <code>getTimezoneOffset()</code> will subtract your time from GMT and return the number of minutes. So if you live in GMT-8, it will return 480.</p>\n\n<p>To put this into hours, divide by 60. Also, notice that the sign is the opposite of what you need - it\'s calculating GMT\'s offset from your time zone, not your time zone\'s offset from GMT. To fix this, simply multiply by -1. </p>\n\n<p>Also note that <a href="http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp" rel="noreferrer">w3school</a> says:</p>\n\n<blockquote>\n  <p>The returned value is not a constant, because of the practice of using\n  Daylight Saving Time.</p>\n</blockquote>\n'}, {'answer_id': 2901216, 'author': 'Alek Davis', 'author_id': 52545, 'author_profile': 'https://Stackoverflow.com/users/52545', 'pm_score': 4, 'selected': False, 'text': '<p>Here is an article (with source code) that explains how to determine and use localized time in an ASP.NET (VB.NET, C#) application: </p>\n\n<p><a href="http://www.windowsitpro.com/article/aspnet2/it-s-about-time.aspx" rel="noreferrer"><strong>It\'s About Time</strong></a></p>\n\n<p>In short, the described approach relies on the JavaScript <code>getTimezoneOffset</code> function, which returns the value that is saved in the session cookie and used by code-behind to adjust time values between GMT and local time. The nice thing is that the user does not need to specify the time zone (the code does it automatically). There is more involved (this is why I link to the article), but provided code makes it really easy to use. I suspect that you can convert the logic to PHP and other languages (as long as you understand ASP.NET). </p>\n'}, {'answer_id': 3724089, 'author': 'Sanjay Khatri', 'author_id': 1026244, 'author_profile': 'https://Stackoverflow.com/users/1026244', 'pm_score': 5, 'selected': False, 'text': '<p>With the PHP <code>date</code> function you will get the date time of server on which the site is located. The only way to get the user time is to use JavaScript.</p>\n\n<p>But I suggest you to, if your site has registration required then the best way is to ask the user while to have registration as a compulsory field. You can list various time zones in the register page and save that in the database. After this, if the user logs in to the site then you can set the default time zone for that session as per the users’ selected time zone.</p>\n\n<p>You can set any specific time zone using the PHP function <code>date_default_timezone_set</code>. This sets the specified time zone for users.</p>\n\n<p>Basically the users’ time zone is goes to the client side, so we must use JavaScript for this.</p>\n\n<p>Below is the script to get users’ time zone using PHP and JavaScript.</p>\n\n<pre><code>&lt;?php\n    #http://www.php.net/manual/en/timezones.php List of Time Zones\n    function showclienttime()\n    {\n        if(!isset($_COOKIE[\'GMT_bias\']))\n        {\n?&gt;\n\n            &lt;script type="text/javascript"&gt;\n                var Cookies = {};\n                Cookies.create = function (name, value, days) {\n                    if (days) {\n                        var date = new Date();\n                        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));\n                        var expires = "; expires=" + date.toGMTString();\n                    }\n                    else {\n                        var expires = "";\n                    }\n                    document.cookie = name + "=" + value + expires + "; path=/";\n                    this[name] = value;\n                }\n\n                var now = new Date();\n                Cookies.create("GMT_bias",now.getTimezoneOffset(),1);\n                window.location = "&lt;?php echo $_SERVER[\'PHP_SELF\'];?&gt;";\n            &lt;/script&gt;\n\n            &lt;?php\n\n        }\n        else {\n          $fct_clientbias = $_COOKIE[\'GMT_bias\'];\n        }\n\n        $fct_servertimedata = gettimeofday();\n        $fct_servertime = $fct_servertimedata[\'sec\'];\n        $fct_serverbias = $fct_servertimedata[\'minuteswest\'];\n        $fct_totalbias = $fct_serverbias – $fct_clientbias;\n        $fct_totalbias = $fct_totalbias * 60;\n        $fct_clienttimestamp = $fct_servertime + $fct_totalbias;\n        $fct_time = time();\n        $fct_year = strftime("%Y", $fct_clienttimestamp);\n        $fct_month = strftime("%B", $fct_clienttimestamp);\n        $fct_day = strftime("%d", $fct_clienttimestamp);\n        $fct_hour = strftime("%I", $fct_clienttimestamp);\n        $fct_minute = strftime("%M", $fct_clienttimestamp);\n        $fct_second = strftime("%S", $fct_clienttimestamp);\n        $fct_am_pm = strftime("%p", $fct_clienttimestamp);\n        echo $fct_day.", ".$fct_month." ".$fct_year." ( ".$fct_hour.":".$fct_minute.":".$fct_second." ".$fct_am_pm." )";\n    }\n\n    showclienttime();\n?&gt;\n</code></pre>\n\n<p>But as per my point of view, it’s better to ask to the users if registration is mandatory in your project.</p>\n'}, {'answer_id': 5492192, 'author': 'Joseph Lust', 'author_id': 564157, 'author_profile': 'https://Stackoverflow.com/users/564157', 'pm_score': 6, 'selected': False, 'text': '<p>Here is a more complete way.</p>\n\n<ol>\n<li>Get the timezone offset for the user</li>\n<li>Test some days on daylight saving boundaries to determine if they are in a zone that uses daylight saving.</li>\n</ol>\n\n<p>An excerpt is below:</p>\n\n<pre><code>function TimezoneDetect(){\n    var dtDate = new Date(\'1/1/\' + (new Date()).getUTCFullYear());\n    var intOffset = 10000; //set initial offset high so it is adjusted on the first attempt\n    var intMonth;\n    var intHoursUtc;\n    var intHours;\n    var intDaysMultiplyBy;\n\n    // Go through each month to find the lowest offset to account for DST\n    for (intMonth=0;intMonth &lt; 12;intMonth++){\n        //go to the next month\n        dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);\n\n        // To ignore daylight saving time look for the lowest offset.\n        // Since, during DST, the clock moves forward, it\'ll be a bigger number.\n        if (intOffset &gt; (dtDate.getTimezoneOffset() * (-1))){\n            intOffset = (dtDate.getTimezoneOffset() * (-1));\n        }\n    }\n\n    return intOffset;\n}\n</code></pre>\n\n<p><a href="https://web.archive.org/web/20140703082827/http://michaelapproved.com/articles/timezone-detect-and-ignore-daylight-saving-time-dst" rel="noreferrer">Getting TZ and DST from JS</a> (via Way Back Machine)</p>\n'}, {'answer_id': 5607229, 'author': 'Westy92', 'author_id': 453314, 'author_profile': 'https://Stackoverflow.com/users/453314', 'pm_score': 5, 'selected': False, 'text': '<p>Using Unkwntech\'s approach, I wrote a function using jQuery and PHP. This is tested and does work!</p>\n\n<p>On the PHP page where you want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:</p>\n\n<pre><code>&lt;?php\n    session_start();\n    $timezone = $_SESSION[\'time\'];\n?&gt;\n</code></pre>\n\n<p>This will read the session variable "time", which we are now about to create.</p>\n\n<p>On the same page, in the &lt;head>, you need to first of all include jQuery:</p>\n\n<pre><code>&lt;script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;\n</code></pre>\n\n<p>Also in the &lt;head>, below the jQuery, paste this:</p>\n\n<pre><code>&lt;script type="text/javascript"&gt;\n    $(document).ready(function() {\n        if("&lt;?php echo $timezone; ?&gt;".length==0){\n            var visitortime = new Date();\n            var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;\n            $.ajax({\n                type: "GET",\n                url: "http://example.org/timezone.php",\n                data: \'time=\'+ visitortimezone,\n                success: function(){\n                    location.reload();\n                }\n            });\n        }\n    });\n&lt;/script&gt;\n</code></pre>\n\n<p>You may or may not have noticed, but you need to change the URL to your actual domain.</p>\n\n<p>One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this:\n(create a new file called <strong>timezone.php</strong> and point to it with the above URL)</p>\n\n<pre><code>&lt;?php\n    session_start();\n    $_SESSION[\'time\'] = $_GET[\'time\'];\n?&gt;\n</code></pre>\n\n<p>If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.</p>\n'}, {'answer_id': 7217925, 'author': 'Envis', 'author_id': 915954, 'author_profile': 'https://Stackoverflow.com/users/915954', 'pm_score': 5, 'selected': False, 'text': '<p>JavaScript:</p>\n\n<pre><code>function maketimus(timestampz)\n{\n    var linktime = new Date(timestampz * 1000);\n    var linkday = linktime.getDate();\n    var freakingmonths = new Array();\n\n    freakingmonths[0]  = "jan";\n    freakingmonths[1]  = "feb";\n    freakingmonths[2]  = "mar";\n    freakingmonths[3]  = "apr";\n    freakingmonths[4]  = "may";\n    freakingmonths[5]  = "jun";\n    freakingmonths[6]  = "jul";\n    freakingmonths[7]  = "aug";\n    freakingmonths[8]  = "sep";\n    freakingmonths[9]  = "oct";\n    freakingmonths[10] = "nov";\n    freakingmonths[11] = "dec";\n\n    var linkmonthnum = linktime.getMonth();\n    var linkmonth = freakingmonths[linkmonthnum];\n    var linkyear = linktime.getFullYear();\n    var linkhour = linktime.getHours();\n    var linkminute = linktime.getMinutes();\n\n    if (linkminute &lt; 10)\n    {\n        linkminute = "0" + linkminute;\n    }\n\n    var fomratedtime = linkday + linkmonth + linkyear + " " +\n                       linkhour + ":" + linkminute + "h";\n    return fomratedtime;\n}\n</code></pre>\n\n<p>Simply provide your times in Unix timestamp format to this function; JavaScript already knows the timezone of the user.</p>\n\n<p>Like this:</p>\n\n<p>PHP:</p>\n\n<pre><code>echo \'&lt;script type="text/javascript"&gt;\nvar eltimio = maketimus(\'.$unix_timestamp_ofshiz.\');\ndocument.write(eltimio);\n&lt;/script&gt;&lt;noscript&gt;pls enable javascript&lt;/noscript&gt;\';\n</code></pre>\n\n<p>This will always show the times correctly based on the timezone the person has set on his/her computer clock. There is no need to ask anything to anyone and save it into places, thank god!</p>\n'}, {'answer_id': 8340357, 'author': 'Naeem Ul Wahhab', 'author_id': 1067051, 'author_profile': 'https://Stackoverflow.com/users/1067051', 'pm_score': 4, 'selected': False, 'text': '<p>A simple way to do it is by using:</p>\n\n<pre><code>new Date().getTimezoneOffset();\n</code></pre>\n'}, {'answer_id': 9043904, 'author': 'Dane Iracleous', 'author_id': 644337, 'author_profile': 'https://Stackoverflow.com/users/644337', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s how I do it. This will set the PHP default timezone to the user\'s local timezone. Just paste the following on the top of all your pages:</p>\n\n<pre><code>&lt;?php\nsession_start();\n\nif(!isset($_SESSION[\'timezone\']))\n{\n    if(!isset($_REQUEST[\'offset\']))\n    {\n    ?&gt;\n        &lt;script&gt;\n        var d = new Date()\n        var offset= -d.getTimezoneOffset()/60;\n        location.href = "&lt;?php echo $_SERVER[\'PHP_SELF\']; ?&gt;?offset="+offset;\n        &lt;/script&gt;\n        &lt;?php   \n    }\n    else\n    {\n        $zonelist = array(\'Kwajalein\' =&gt; -12.00, \'Pacific/Midway\' =&gt; -11.00, \'Pacific/Honolulu\' =&gt; -10.00, \'America/Anchorage\' =&gt; -9.00, \'America/Los_Angeles\' =&gt; -8.00, \'America/Denver\' =&gt; -7.00, \'America/Tegucigalpa\' =&gt; -6.00, \'America/New_York\' =&gt; -5.00, \'America/Caracas\' =&gt; -4.30, \'America/Halifax\' =&gt; -4.00, \'America/St_Johns\' =&gt; -3.30, \'America/Argentina/Buenos_Aires\' =&gt; -3.00, \'America/Sao_Paulo\' =&gt; -3.00, \'Atlantic/South_Georgia\' =&gt; -2.00, \'Atlantic/Azores\' =&gt; -1.00, \'Europe/Dublin\' =&gt; 0, \'Europe/Belgrade\' =&gt; 1.00, \'Europe/Minsk\' =&gt; 2.00, \'Asia/Kuwait\' =&gt; 3.00, \'Asia/Tehran\' =&gt; 3.30, \'Asia/Muscat\' =&gt; 4.00, \'Asia/Yekaterinburg\' =&gt; 5.00, \'Asia/Kolkata\' =&gt; 5.30, \'Asia/Katmandu\' =&gt; 5.45, \'Asia/Dhaka\' =&gt; 6.00, \'Asia/Rangoon\' =&gt; 6.30, \'Asia/Krasnoyarsk\' =&gt; 7.00, \'Asia/Brunei\' =&gt; 8.00, \'Asia/Seoul\' =&gt; 9.00, \'Australia/Darwin\' =&gt; 9.30, \'Australia/Canberra\' =&gt; 10.00, \'Asia/Magadan\' =&gt; 11.00, \'Pacific/Fiji\' =&gt; 12.00, \'Pacific/Tongatapu\' =&gt; 13.00);\n        $index = array_keys($zonelist, $_REQUEST[\'offset\']);\n        $_SESSION[\'timezone\'] = $index[0];\n    }\n}\n\ndate_default_timezone_set($_SESSION[\'timezone\']);\n\n//rest of your code goes here\n?&gt;\n</code></pre>\n'}, {'answer_id': 10775887, 'author': 'JoeyFur62', 'author_id': 1420406, 'author_profile': 'https://Stackoverflow.com/users/1420406', 'pm_score': 5, 'selected': False, 'text': '<p>Easy, just use the JavaScript <code>getTimezoneOffset</code> function like so:</p>\n\n<pre><code>-new Date().getTimezoneOffset()/60;\n</code></pre>\n'}, {'answer_id': 11717580, 'author': 'pckabeer', 'author_id': 1298003, 'author_profile': 'https://Stackoverflow.com/users/1298003', 'pm_score': 4, 'selected': False, 'text': '<p>Try this PHP code:</p>\n\n<pre><code>&lt;?php\n    $ip = $_SERVER[\'REMOTE_ADDR\'];\n    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=" . $ip . "&amp;full=true");\n    $json = json_decode($json,true);\n    $timezone = $json[\'LocalTimeZone\'];\n?&gt;\n</code></pre>\n'}, {'answer_id': 11836123, 'author': 'Adam', 'author_id': 226513, 'author_profile': 'https://Stackoverflow.com/users/226513', 'pm_score': 6, 'selected': False, 'text': '<p>Here is a robust JavaScript solution to determine the time zone the browser is in.</p>\n<pre><code>&gt;&gt;&gt; var timezone = jstz.determine();\n&gt;&gt;&gt; timezone.name(); \n&quot;Europe/London&quot;\n</code></pre>\n<p><a href="https://github.com/pellepim/jstimezonedetect" rel="nofollow noreferrer">https://github.com/pellepim/jstimezonedetect</a></p>\n'}, {'answer_id': 12190240, 'author': 'Benbob', 'author_id': 181637, 'author_profile': 'https://Stackoverflow.com/users/181637', 'pm_score': 5, 'selected': False, 'text': '<p>I still have not seen a detailed answer here that gets the time zone. You shouldn\'t need to geocode by IP address or use PHP (lol) or incorrectly guess from an offset. </p>\n\n<p>Firstly a time zone is not just an offset from GMT. It is an area of land in which the time rules are set by local standards. Some countries have daylight savings, and will switch on DST at differing times. It\'s usually important to get the actual zone, not just the current offset.</p>\n\n<p>If you intend to store this timezone, for instance in user preferences you want the zone and not just the offset. For realtime conversions it won\'t matter much.</p>\n\n<p>Now, to get the time zone with javascript you can use this:</p>\n\n<pre><code>&gt;&gt; new Date().toTimeString();\n"15:46:04 GMT+1200 (New Zealand Standard Time)"\n//Use some regular expression to extract the time.\n</code></pre>\n\n<p>However I found it easier to simply use this robust plugin which returns the Olsen formatted timezone:</p>\n\n<p><a href="https://github.com/scottwater/jquery.detect_timezone" rel="noreferrer">https://github.com/scottwater/jquery.detect_timezone</a></p>\n'}, {'answer_id': 12398468, 'author': 'philfreo', 'author_id': 137067, 'author_profile': 'https://Stackoverflow.com/users/137067', 'pm_score': 5, 'selected': False, 'text': '<p>To submit the timezone offset as an HTTP header on AJAX requests with <a href="http://jquery.com/" rel="noreferrer">jQuery</a></p>\n\n<pre><code>$.ajaxSetup({\n    beforeSend: function(xhr, settings) {\n        xhr.setRequestHeader("X-TZ-Offset", -new Date().getTimezoneOffset()/60);\n    }\n});\n</code></pre>\n\n<p>You can also do something similar to get the actual time zone name by using <code>moment.tz.guess();</code> from <a href="http://momentjs.com/timezone/docs/#/using-timezones/guessing-user-timezone/" rel="noreferrer">http://momentjs.com/timezone/docs/#/using-timezones/guessing-user-timezone/</a></p>\n'}, {'answer_id': 14126931, 'author': 'Jaime G', 'author_id': 1943570, 'author_profile': 'https://Stackoverflow.com/users/1943570', 'pm_score': 5, 'selected': False, 'text': "<p>Don't use the IP address to definitively determine location (and hence timezone)-- that's because with NAT, proxies (increasingly popular), and VPNs, IP addresses do not necessarily realistically reflect the user's actual location, but the location at which the servers implementing those protocols reside.</p>\n\n<p>Similar to how US area codes are no longer useful for locating a telephone user, given the popularity of number portability.</p>\n\n<p>IP address and other techniques shown above are useful for <strong>suggesting a default</strong> that the user can adjust/correct.</p>\n"}, {'answer_id': 22625076, 'author': 'Matt Johnson-Pint', 'author_id': 634824, 'author_profile': 'https://Stackoverflow.com/users/634824', 'pm_score': 7, 'selected': False, 'text': '<p>First, understand that time zone detection in JavaScript is imperfect.  You can get the local <em>time zone offset</em> for a particular date and time using <code>getTimezoneOffset</code> on an instance of the <code>Date</code> object, but that\'s not quite the same as a full <a href="https://iana.org/time-zones" rel="noreferrer">IANA time zone</a> like <code>America/Los_Angeles</code>.</p>\n<p>There are some options that can work though:</p>\n<ul>\n<li>Most modern browsers support IANA time zones in their implementation of the <a href="https://www.ecma-international.org/ecma-402/" rel="noreferrer">ECMAScript Internationalization API</a>, so you can do this:</li>\n</ul>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const tzid = Intl.DateTimeFormat().resolvedOptions().timeZone;\nconsole.log(tzid);</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<p>The result is a string containing the IANA time zone setting of the computer where the code is running.</p>\n<p>Supported environments are listed <a href="https://kangax.github.io/compat-table/esintl/#test-DateTimeFormat_resolvedOptions().timeZone_defaults_to_the_host_environment" rel="noreferrer">in the Intl compatibility table</a>. Expand the <code>DateTimeFormat</code> section, and look at the feature named <code>resolvedOptions().timeZone defaults to the host environment</code>.</p>\n<ul>\n<li><p>Some libraries, such as <a href="https://moment.github.io/luxon" rel="noreferrer">Luxon</a> use this API to determine the time zone through functions like <code>luxon.Settings.defaultZoneName</code>.</p>\n</li>\n<li><p>If you need to support an wider set of environments, such as older web browsers, you can use a library to make an <em>educated guess</em> at the time zone.  They work by first trying the <code>Intl</code> API if it\'s available, and when it\'s not available, they interrogate the <code>getTimezoneOffset</code> function of the <code>Date</code> object, for several different points in time, using the results to choose an appropriate time zone from an internal data set.</p>\n<p>Both <a href="https://github.com/pellepim/jstimezonedetect" rel="noreferrer">jsTimezoneDetect</a> and <a href="https://momentjs.com/timezone" rel="noreferrer">moment-timezone</a> have this functionality.</p>\n<pre><code>  // using jsTimeZoneDetect\n  var tzid = jstz.determine().name();\n\n  // using moment-timezone\n  var tzid = moment.tz.guess();\n</code></pre>\n<p>In both cases, the result can only be thought of as a guess.  The guess may be correct in many cases, but not all of them.</p>\n<p>Additionally, these libraries have to be periodically updated to counteract the fact that many older JavaScript implementations are only aware of the <em>current</em> daylight saving time rule for their local time zone.  <a href="https://codeofmatt.com/2013/06/07/javascript-date-type-is-horribly-broken/" rel="noreferrer">More details on that here.</a></p>\n</li>\n</ul>\n<p>Ultimately, a better approach is to actually ask your user for their time zone.  Provide a setting that they can change.  You can use one of the above options to choose a <em>default</em> setting, but don\'t make it impossible to deviate from that in your app.</p>\n<p>There\'s also the entirely different approach of <em>not</em> relying on the time zone setting of the user\'s computer at all.  Instead, if you can gather latitude and longitude coordinates, you can resolve those to a time zone using <a href="https://stackoverflow.com/a/16086964/634824">one of these methods</a>.  This works well on mobile devices.</p>\n'}, {'answer_id': 25734579, 'author': 'Berislav Lopac', 'author_id': 122033, 'author_profile': 'https://Stackoverflow.com/users/122033', 'pm_score': 3, 'selected': False, 'text': '<p>One possible option is to use the <code>Date</code> header field, which is defined in <a href="https://www.rfc-editor.org/rfc/rfc7231#page-65" rel="nofollow noreferrer">RFC 7231</a> and is supposed to include the timezone. Of course, it is not guaranteed that the value is really the client\'s timezone, but it can be a convenient starting point.</p>\n'}, {'answer_id': 25839604, 'author': 'man', 'author_id': 1881655, 'author_profile': 'https://Stackoverflow.com/users/1881655', 'pm_score': 4, 'selected': False, 'text': '<p>It is simple with JavaScript and PHP:</p>\n\n<p>Even though the user can mess with his/her internal clock and/or timezone, the best way I found so far, to get the offset, remains <code>new Date().getTimezoneOffset();</code>. It\'s non-invasive, doesn\'t give head-aches and eliminates the need to rely on third parties.</p>\n\n<p>Say I have a table, <code>users</code>, that contains a field <code>date_created int(13)</code>, for storing Unix timestamps;</p>\n\n<p>Assuming a client <code>creates a new account</code>, data is received by <code>post</code>, and I need to <code>insert/update</code> the <code>date_created column</code> with the client\'s Unix timestamp, not the server\'s.</p>\n\n<p>Since the timezoneOffset is needed at the time of insert/update, it is passed as an extra $_POST element when the client submits the form, thus eliminating the need to store it in sessions and/or cookies, and no additional server hits either.</p>\n\n<pre><code>var off = (-new Date().getTimezoneOffset()/60).toString();//note the \'-\' in front which makes it return positive for negative offsets and negative for positive offsets\nvar tzo = off == \'0\' ? \'GMT\' : off.indexOf(\'-\') &gt; -1 ? \'GMT\'+off : \'GMT+\'+off;\n</code></pre>\n\n<p>Say the server receives <code>tzo</code> as <code>$_POST[\'tzo\']</code>;</p>\n\n<pre><code>$ts = new DateTime(\'now\', new DateTimeZone($_POST[\'tzo\']);\n$user_time = $ts-&gt;format("F j, Y, g:i a");//will return the users current time in readable format, regardless of whether date_default_timezone() is set or not.\n$user_timestamp = strtotime($user_time);\n</code></pre>\n\n<p>Insert/update <code>date_created=$user_timestamp</code>.</p>\n\n<p>When retrieving the date_created, you can convert the timestamp like so:</p>\n\n<pre><code>$date_created = // Get from the database\n$created = date("F j, Y, g:i a",$date_created); // Return it to the user or whatever\n</code></pre>\n\n<p>Now, this example may fit one\'s needs, when it comes to inserting a <code>first</code> timestamp... When it comes to an additional timestamp, or table, you may want to consider inserting the tzo value into the users table for future reference, or setting it as session or as a cookie.</p>\n\n<p>P.S. BUT what if the user travels and switches timezones. Logs in at GMT+4, travels fast to GMT-1 and logs in again. Last login would be in the future.</p>\n\n<p>I think... we think too much.</p>\n'}, {'answer_id': 39463380, 'author': 'Tomas Tomecek', 'author_id': 909579, 'author_profile': 'https://Stackoverflow.com/users/909579', 'pm_score': 4, 'selected': False, 'text': '<p>You could do it on the client with <a href="http://momentjs.com/timezone/" rel="noreferrer">moment-timezone</a> and send the value to server; sample usage:</p>\n\n<pre><code>&gt; moment.tz.guess()\n"America/Asuncion"\n</code></pre>\n'}, {'answer_id': 41568276, 'author': 'Useful Angle', 'author_id': 7191417, 'author_profile': 'https://Stackoverflow.com/users/7191417', 'pm_score': 4, 'selected': False, 'text': '<p>Getting a valid TZ Database timezone name in PHP is a two-step process:</p>\n\n<ol>\n<li><p>With JavaScript, get timezone offset in minutes through <code>getTimezoneOffset</code>. This offset will be positive if the local timezone is behind UTC and negative if it is ahead. So you must add an opposite sign to the offset.</p>\n\n<pre><code>var timezone_offset_minutes = new Date().getTimezoneOffset();\ntimezone_offset_minutes = timezone_offset_minutes == 0 ? 0 : -timezone_offset_minutes;\n</code></pre>\n\n<p>Pass this offset to PHP.</p></li>\n<li><p>In PHP convert this offset into a valid timezone name with <em>timezone_name_from_abbr</em> function.</p>\n\n<pre><code>// Just an example.\n$timezone_offset_minutes = -360;  // $_GET[\'timezone_offset_minutes\']\n\n// Convert minutes to seconds\n$timezone_name = timezone_name_from_abbr("", $timezone_offset_minutes*60, false);\n\n// America/Chicago\necho $timezone_name;&lt;/code&gt;&lt;/pre&gt;\n</code></pre></li>\n</ol>\n\n<p>I\'ve written a blog post on it: <em><a href="http://usefulangle.com/post/31/detect-user-browser-timezone-name-in-php" rel="noreferrer">How to Detect User Timezone in PHP</a></em>. It also contains a demo.</p>\n'}, {'answer_id': 64329945, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>There's no such way to figure the timezone in the actual HTML code or any <code>user-agent</code> string, but what you can do is make a basic function getting it using JavaScript.</p>\n<p>I don't know how to code with JavaScript yet so my function might take time to make.</p>\n<p>However, you can try to get the actual timezone also using JavaScript with the <code>getTzimezoneOffset()</code> function in the <code>Date</code> section or simply <code>new Date().getTimezoneOffset();</code>.</p>\n"}, {'answer_id': 67072466, 'author': 'Antony', 'author_id': 3897214, 'author_profile': 'https://Stackoverflow.com/users/3897214', 'pm_score': 1, 'selected': False, 'text': '<p>I think that <code>@Matt Johnson-Pints</code> is by far the best and a CanIuse search reveals that now it is widely adopted:</p>\n<p><a href="https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone" rel="nofollow noreferrer">https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone</a></p>\n<p>One of the challenges though is to consider why you want to know the Timezone. Because I think one of the things most people have missed is that <strong>they can change!</strong> If a user travels with his laptop from Europe to America if you had previously stored it in a database their timezone is now incorrect (even if the user never actually updates their devices timezone). This is also the problem with <code>@Mads Kristiansen</code> answer as well because users travel - you cannot rely on it as a given.</p>\n<p>For example, my Linux laptop has &quot;automatic timezone&quot; turned off. Whilst the time might update my timezone doesn\'t.</p>\n<p>So I believe the answer is - what do you need it for? Client side certainly seems to give an easier way to ascertain it, but both client and server side code will depend on either the user updating their timezone or it updating automatically. I might of course be wrong.</p>\n'}, {'answer_id': 70495118, 'author': 'Atul Lohiya', 'author_id': 9942276, 'author_profile': 'https://Stackoverflow.com/users/9942276', 'pm_score': 2, 'selected': False, 'text': '<p>There can be a few ways to determine the timezone in the browser. If there is a standard function that is available and supported by your browser, that is what you should use. Below are three ways to get the same information in different formats. Avoid using non-standard solutions that make any guesses based on certain assumptions or hard coded lists of zones though they may be helpful if nothing else can be done.</p>\n<p>Once you have this info, you can pass this as a non-standard request header to server and use it there. If you also need the timezone offset, you can also pass it to server in headers or in request payload which can be retrieved with dateObj.getTimezoneOffset().</p>\n<ol>\n<li><strong>Use Intl API to get the Olson format (Standard and recommended way)</strong>: Note that this is not supported by all browsers. Refer <a href="https://caniuse.com/?search=Intl.DateTimeFormat().resolvedOptions().timeZone" rel="nofollow noreferrer">this link</a> for details on browser support for this.\nThis API let\'s you get the timezone in Olson format i.e., something like Asia/Kolkata, America/New_York etc.</li>\n</ol>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>Intl.DateTimeFormat().resolvedOptions().timeZone</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<ol start="2">\n<li><strong>Use Date object to get the long format</strong> such as India Standard Time, Eastern Standard Time etc: This is supported by all browsers.</li>\n</ol>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>let dateObj = new Date(2021, 11, 25, 09, 30, 00);\n\n//then \n\ndateObj.toString() \n\n//yields\n\nSat Dec 25 2021 09:30:00 GMT+0530 (India Standard Time) //I am located in India (IST)</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<p>Notice the string contains timezone info in long and short formats. You can now use regex to get this info out:</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>let longZoneRegex = /\\((.+)\\)/;\ndateObj.toString().match(longZoneRegex);\n\n//yields\n\n[\'(India Standard Time)\', \'India Standard Time\', index: 34, input: \'Sat Dec 25 2021 09:30:00 GMT+0530 (India Standard Time)\', groups: undefined]\n\n//Note that output is an array so use output[1] to get the timezone name.</code></pre>\r\n</div>\r\n</div>\r\n</p>\n<ol start="3">\n<li><strong>Use Date object to get the short format</strong> such as GMT+0530, GMT-0500 etc: This is supported by all browsers.</li>\n</ol>\n<p>Similarly, you can get the short format out too:</p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>let shortZoneRegex = /GMT[+-]\\d{1,4}/;\ndateObj.toString().match(shortZoneRegex);\n\n//yields\n\n[\'GMT+0530\', index: 25, input: \'Sat Dec 25 2021 09:30:00 GMT+0530 (India Standard Time)\', groups: undefined]\n\n//Note that output is an array so use output[0] to get the timezone name.</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}]
['https://Stackoverflow.com/questions/13', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9/']