Viewed   62 times

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.



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

<script type="text/javascript" src="//">
<script type="text/javascript" src="//">
<script type="text/javascript">
    var tz = jstz.determine(); // Determines the time zone of the browser client
    var timezone =; //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.
Tuesday, October 18, 2022

According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:

* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)

UTC is the default if all of those fail, so that's probably a good starting point.

Friday, August 19, 2022

Unfortunately this information is not passed in HTTP headers.

Usually you need cooperating JavaScript to fetch it for you.
Web is full of examples, here is one

Wednesday, December 21, 2022

You can't do this in PHP alone.

You can use Javascript to set the value in a cookie, then use PHP to read the cookie on the next page (re)load.


var dateVar = new Date()
var offset = dateVar.getTimezoneOffset();
document.cookie = "offset="+offset;


echo $_COOKIE['offset'];

Use this to convert the offset to the friendly timezone name in PHP. Javascript returns the offset in minutes, while this PHP function expects the input to be in seconds - so multiply by 60. The third parameter is a boolean value of whether or not you are in Daylight Savings Time. Read the manual and update the code to fit your needs.

echo timezone_name_from_abbr("", intval($_COOKIE['offset'])*60, 0);

Monday, November 21, 2022

Try ACTION_USER_FOREGROUND and ACTION_USER_BACKGROUND. I have not used them, but they were added in API Level 17, and their description seems like it may help.

Thursday, August 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :