user6464914
user6464914

Reputation:

How to autodetect timezone with php or use external website

Hello i am using time now value to store time which dependent on server time but my website is to be used by all people all over the world so i am trying to detect the users timezone automatically. Thanks in advance

Upvotes: 5

Views: 776

Answers (4)

Chris Lim
Chris Lim

Reputation: 424

You may use the IP geolocation technology to get the visitor's location from the IP address and then get the time zone value. Although IP geolocation may not 100% accurate in location query but this shall be the best non-intrusive solution you may get.

You may download the free IP to time zone database at http://lite.ip2location.com if you were interested in this method.

Upvotes: 0

Sherif
Sherif

Reputation: 11943

In most cases you shouldn't care about or need to know the user's timezone in order for your website to function. Typically you store everything in UTC (Unix time stamp) on the server and transport it to the client as such. Then, using javascript, you let the user's client UA deal with the timezone conversion locally.

So in PHP something like this.

<?php
$date = new DateTime("2016-09-18 4:45 AM", new DateTimezone("America/New_York"));
$timeStamp = $date->getTimeStamp();
?>
<script>
    var timeStamp = <?=json_encode($timeStamp * 1000)?>;
    alert(new Date(timeStamp);
</script>

The user sees the appropriately formatted date in their browser, according to their timezone, and the server doesn't know or care about that timezone since it uses a timezone agnostic time stamp for transport.

However, if you wish to obtain the user's timezone reliably for any reason other than transport (i.e. you need to apply the conversion on the server-side) then it's best to just ask the user. All HTML5 capable browsers now have an Geolocation API that supports collecting accurate geolocation information from the client UA (this works on mobile too since you can't rely on geolocation IP information for this in mobile devices). Also there some JavaScript libraries like timezone-js which can get the timezone accurately according to the timezone db, which PHP relies on.

The important thing to remember when doing this is that a GMT offset is not enough to know the timezone, especially in PHP. Because PHP relies on the Olson timezone database identifier which provides more than just the GMT offset. For example, in both Toronto Canada and America, New York the current GMT offset is -0400. However, come day light savings time, the GMT offset will be -0500 in both time zones.

Then there are even stranger ones like Phoenix and Denver, two cities in the same state (Arizona) where one observes DST and the other does not. So depending on which part of the year you're in they may either fall in the same GMT offset or they may not.

$denver = new DateTime("now", new DateTimezone("America/Denver"));
$phoenix = new DateTime("now", new DateTimezone("America/Phoenix"));

var_dump($denver->format('c') , $phoenix->format('c'));

$denver->add(new DateInterval("P4M"));
$phoenix->add(new DateInterval("P4M"));
var_dump($denver->format('c'), $phoenix->format('c'));

Output from above code gives us...

string(25) "2016-09-18T03:12:15-06:00"
string(25) "2016-09-18T02:12:15-07:00"
string(25) "2017-01-18T03:12:15-07:00"
string(25) "2017-01-18T02:12:15-07:00"

So unless you want to fall into the trap of losing valuable timezone information by storing the GMT offset and applying that incorrectly at different times, it's best to store the Olson tz identifier instead, that PHP can use to accurately apply timezone conversions without lossy information. The timezone-js library can help you with that since it relies on the same timezone identifiers from the tz db that PHP does.

Upvotes: 1

Bhavin
Bhavin

Reputation: 2158

You can do it with java script :

The common JavaScript that is used to detect a visitor’s timezone is:

var myDate = new Date();
document.write(myDate.getTimezoneOffset());

There are basically two things needed to figure out a visitors time zone. First, we need to determine the time offset from Greenwich Mean Time (GMT). This can easily be done by creating two dates (one local, and one in GMT) and comparing the time difference between them:

var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);

The second thing that you need to know is whether the location observes daylight savings time (DST) or not. Since DST is always observed during the summer, we can compare the time offset between two dates in January, to the time offset between two dates in June. If the offsets are different, then we know that the location observes DST. If the offsets are the same, then we know that the location DOES NOT observe DST.

var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
temp = june1.toGMTString();
var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
var dst;
if (std_time_offset == daylight_time_offset) {
    dst = "0"; // daylight savings time is NOT observed
} else {
    dst = "1"; // daylight savings time is observed
}

Upvotes: 0

user2226755
user2226755

Reputation: 13173

Javascript:

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

PHP:

echo $_COOKIE['offset'];

Original : Get user timezone string with Javascript / PHP?

Upvotes: 0

Related Questions