How to get user timezone using jquery?
Solution 1:
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
Solution 2:
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'];
?>
Solution 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.
Solution 4:
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.