8

I would like to get their timezone with names like 'Asia/Calcutta' or 'Australia/Darwin'

I have php installed on myserver and its time set to 'UTC'

user967144
  • 381
  • 2
  • 4
  • 7

6 Answers6

14

For detecting timezone offset you can use this function:

function get_time_zone_offset( ) {
  var current_date = new Date();
  return parseInt(-current_date.getTimezoneOffset() / 60);
}

Example you can see here

gloomy.penguin
  • 5,563
  • 6
  • 30
  • 58
ValeriiVasin
  • 8,240
  • 11
  • 53
  • 75
13

This is my first post here but hopefully I can help someone. I felt compelled because this site has helped me a lot. Thanks stackoverflow members!

http://www.pageloom.com/automatic-timezone-detection-with-javascript

From what I understand about this JavaScript code it is very accurate and will be able to return the offset from UST, the corresponding Olson Database timezone name, and handle the daylight savings time issue (ex. -5:00, America/New_york, true).

http://www.php.net/manual/en/timezones.america.php

The above link is just a list of the php supported timezone parameters, which as far as I can tell align with this JavaScript code (I am sure that is beneficial I just don't know why yet I am still writing my code and I am kinda new to php/mysql).

The only hurdle you will face after getting this code working on your html page will likely be getting these values to php and then to mysql if that is what you need. I achieved this by sending these values as a $.post using JQuery. I think this is the easiest way to do it (please someone correct me if I am wrong). I believe the other alternatives are using an AJAX command (more complicated) or cookies(I think?).

After I got the values from the Java-script code to the server (PHP) I stored them as session variables so they would change if the user on my site logged in from a different timezone then usual. However they could easily be saved to the database as well.

If you use the JQuery route this needs to be in the header of the html document where the $.post is being executed.

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

The most updated timezonedetect script can be found linked on that first url I posted.

//replace this comment with the most updated timezonedetect code from that first link
var timezone = jstz.determine_timezone();
var tzoffset = timezone.offset();
var tzname = timezone.name();
var tzdst = timezone.dst();
$.post("scripts/tzdetect.php", { tzoffset: tzoffset, tzname: tzname, tzdst: tzdst } );

The receiving file in this case tzdetect.php should look like this if you want the data stored as session variables.

<?php
session_start();
$_SESSION['tzname'] = $_POST['tzname'];
$_SESSION['tzoffset'] = $_POST['tzoffset'];
$_SESSION['tzdst'] = $_POST['tzdst'];
?>
Scott
  • 161
  • 1
  • 6
3

Your question is really about Javascript rather than PHP - because it depends on the client capabilities.

As I understand it, Javascript allows you to determine the current time zone offset (the difference between UTC and local time) but that does not allow you to unambiguously determine the time zone. Many time zones will share the same offset at any one particular point in time, but will differ at other times, in terms of whether they observe daylight saving and at what point they change into and out of daylight saving.

If you definitely need the time zone, you might want to work out a list of possible time zones, and present that to the user - also allowing them to override the narrowing decision, choosing any time zone.

Again, it really depends on what you're trying to achieve.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Where can i get that list of timezones for storing in php array? lets say if the offset is "new Date().getTimezoneOffset()/60" ..is that value available in my array? – user967144 Nov 11 '11 at 07:17
  • 1
    @user967144: In what array? It's not really clear what you're asking. To determine the possible time zones, you should iterate over *all* time zones and find ones with the same offset. You can find all the time zones using [`DateTimeZone::listIdentifiers`](http://www.php.net/manual/en/datetimezone.listidentifiers.php). – Jon Skeet Nov 11 '11 at 07:21
  • i found a link here could you have a look at the function posted by BrightNail? codingforums.com/showthread.php?t=139888 – user967144 Nov 11 '11 at 07:25
  • @user967144: That's neither complete nor authoritative. There can be multiple time zones which have the same daylight offset, the same standard offset, but change at different times. – Jon Skeet Nov 11 '11 at 07:31
  • So whats the best way? What i am really looking for is to know the browsers timezone name so i can modify my schedule time for them. I must need timezone names, cause i'm using DateTime function in php – user967144 Nov 11 '11 at 07:35
  • @user967144: As I wrote in my answer: ask the user, with suggestions based on knowledge you *can* get from the user. Javascript simply doesn't give you that information. You *could* do a very complicated sort of search with repeated round-trips to the server narrowing the possibilities down - but it would be very hard to get right, and would rely on the browser and server having the same historical information about time zones, which they may not. The idea in that codingforums thread of picking multiple times to narrow down is good - it's just not enough. – Jon Skeet Nov 11 '11 at 07:37
3

You do not need jQuery to do this. Try the following code:

var offset = (new Date()).getTimezoneOffset();
alert(offset);

It will alert current timezone offset from the browser (in minutes).

See this jsfiddle as a proof.

I think you will need to convert it to the timezone name on your own. If you want the names to be consistent, you should not depend on the user's browser.

You can use this code (see this jsfiddle):

var offset = (new Date()).getTimezoneOffset();

var timezones = {
    '-12': 'Pacific/Kwajalein',
    '-11': 'Pacific/Samoa',
    '-10': 'Pacific/Honolulu',
    '-9': 'America/Juneau',
    '-8': 'America/Los_Angeles',
    '-7': 'America/Denver',
    '-6': 'America/Mexico_City',
    '-5': 'America/New_York',
    '-4': 'America/Caracas',
    '-3.5': 'America/St_Johns',
    '-3': 'America/Argentina/Buenos_Aires',
    '-2': 'Atlantic/Azores',
    '-1': 'Atlantic/Azores',
    '0': 'Europe/London',
    '1': 'Europe/Paris',
    '2': 'Europe/Helsinki',
    '3': 'Europe/Moscow',
    '3.5': 'Asia/Tehran',
    '4': 'Asia/Baku',
    '4.5': 'Asia/Kabul',
    '5': 'Asia/Karachi',
    '5.5': 'Asia/Calcutta',
    '6': 'Asia/Colombo',
    '7': 'Asia/Bangkok',
    '8': 'Asia/Singapore',
    '9': 'Asia/Tokyo',
    '9.5': 'Australia/Darwin',
    '10': 'Pacific/Guam',
    '11': 'Asia/Magadan',
    '12': 'Asia/Kamchatka' 
};

alert(timezones[-offset / 60]);

However I would not rely on this too much.

Tadeck
  • 117,059
  • 25
  • 140
  • 191
  • nice, it alerts "-330".. now how can i modify it to show 'Asia/Calcutta' ? – user967144 Nov 11 '11 at 07:19
  • @user967144: See updated version, this now popups the name you expect. – Tadeck Nov 11 '11 at 07:32
  • @user967144: No, it is not reliable - it is based on the offset, so the timezone will change depending on whether daylight saving time is in effect or not. But I do not see other option, if you want to rely on data from the browser. This is the best solution. – Tadeck Nov 11 '11 at 07:45
  • An other way to do this is to compare the computer timestamp (using date in javascript) with a timestamp from the server. You can then calculate the difference. I've seen this function `getTimezoneOffset()` fail every now and then – OptimusCrime Nov 11 '11 at 07:46
  • @OptimusCrime: What I am telling is that **determining timezone based on the offset** is **unreliable**. Thus your solution is as unreliable as mine. You will not solve the problem of daylight saving time. – Tadeck Nov 11 '11 at 07:50
  • @OptimusCrime: Does the solution meet your needs? – Tadeck Nov 13 '11 at 12:43
  • Well, in my eyes, there is no better way to do it. You could try to lookup if the place has a daylight saving time or not, but this is a big job. – OptimusCrime Nov 14 '11 at 10:50
  • 2
    It's not "unreliable", it's plain wrong. A timezone is a set of rules applied to a certain region. The time offset depends on the time of the year, and on the current set of rules (e.g. Russia recently abandoned daylight savings). There is no way to guess a time zone from just one offset. jstz does this pretty well, and it does a lot more probing. – Martin Jambon Mar 19 '14 at 18:50
  • @MartinJambon: I agree on what you say about offset vs. time zone - you are completely right, and rather offset depends on several variables, including time zone, than the other way around. However, I do not know jstz and cannot give my opinion on that. Anyway, upvote for the comment. – Tadeck Mar 20 '14 at 02:11
0

You can get the timezone offset. Which you can use to calculate the approximate timezone. You can keep an associative list of timezone offset and the names. This method does not give any exact values, though.

abhinav
  • 2,979
  • 1
  • 18
  • 25
  • You can't work out the time zone from the time zone offset. You can work out a list of *possible* time zones, but there may well be more than one. And you say it's "not needed" but that entirely depends on what the OP needs to do. For example, to work out what the local time will be in 10 minutes, a time zone offset isn't enough. – Jon Skeet Nov 11 '11 at 07:10
  • @JonSkeet Thanks! You're absolutely right, you can only get the approximate timezone from the offset. Editing the answer. – abhinav Nov 11 '11 at 07:23
-2
<?
echo date("e");
?>

This will give the output:

Asia/Kolkata

Santhosh J
  • 330
  • 2
  • 13