PHP 8.1 - Laravel - Geoip find timezone by country and region - php

I want to find the timezone with the country and region using
geoip_time_zone_by_country_and_region()
but seems it needs geoip ext to be installed which is not supported anymore
and seems geoip2 just works with IP but I want to get the timezone by country and region(state) name
is there any solution?
I'm using Laravel 9 and PHP 8.1

2 choices
Get free versions of maxmind geoip (Geolite DB) and use them . Cumbersome for just the zone
Use javascript https://www.geeksforgeeks.org/how-to-set-timezone-offset-using-javascript/ ..offset example
Or if you want the actual zone text:
var justforRefTz ; justforRefTz = Intl.DateTimeFormat().resolvedOptions().timeZone;

The geoip_time_zone_by_country_and_region function uses hardcoded data from this file, which is deprecated and hasn't had any time zone data updated since 2013. You should definitely not use it any more.
Additionally, that function by its definition alone is impossible to accurately implement, as there are many regions that use different time zones in different places within the region. Just in the USA alone, there are 16 states that have more than one time zone.
As an example, the US state of South Dakota is split almost down the middle between the Mountain and Central time zones.
geoip_time_zone_by_country_and_region("US", "SD") is hardcoded to return "America/Chicago" - which is the IANA time zone identifier for US Central Time - which applies in the eastern half of South Daktoa only. The western half should use "America/Denver" instead.
Any approach to resolving a time zone needs to take much more than just country and region into account. A more accurate approach would be to use latitude and longitude coordinates, which could be resolved from an address (or at least a country, region, and city) via a number of different techniques.

Related

Moment JS has 100 or so more timezones compared to PHP, bug? How do I get them in PHP

I'm using Moment JS, and have just recently outputted the timezones listed using this.$moment.tz.names() in my Nuxt JS project. However, the timezones that my server has inside of the timezone_identifiers_list function in PHP seems to be about 100 or so less, and weirdly, it seems that some important ones are either missing from PHP, or not meant to be in Moment, such as:
US/Central
Why would PHP not contain these missing timezones from Moment?
I'll attach a screenshot of the ones that appear to be outputted from Moment that aren't in PHP, wondering how I can get these timezones into that PHP list?
I've outputted the list of timezones from PHP into a string, because I'm going to have to compare the moment ones then and set a default if my timezone guess logic picks one that doesn't exist since I have a Laravel validation rule for timezone.
const prodTimezones = JSON.parse("[\"Africa\\/Abidjan\",\"Africa\\/Accra\",\"Africa\\/Addis_Ababa\",\"Africa\\/Algiers\",\"Africa\\/Asmara\",\"Africa\\/Bamako\",\"Africa\\/Bangui\",\"Africa\\/Banjul\",\"Africa\\/Bissau\",\"Africa\\/Blantyre\",\"Africa\\/Brazzaville\",\"Africa\\/Bujumbura\",\"Africa\\/Cairo\",\"Africa\\/Casablanca\",\"Africa\\/Ceuta\",\"Africa\\/Conakry\",\"Africa\\/Dakar\",\"Africa\\/Dar_es_Salaam\",\"Africa\\/Djibouti\",\"Africa\\/Douala\",\"Africa\\/El_Aaiun\",\"Africa\\/Freetown\",\"Africa\\/Gaborone\",\"Africa\\/Harare\",\"Africa\\/Johannesburg\",\"Africa\\/Juba\",\"Africa\\/Kampala\",\"Africa\\/Khartoum\",\"Africa\\/Kigali\",\"Africa\\/Kinshasa\",\"Africa\\/Lagos\",\"Africa\\/Libreville\",\"Africa\\/Lome\",\"Africa\\/Luanda\",\"Africa\\/Lubumbashi\",\"Africa\\/Lusaka\",\"Africa\\/Malabo\",\"Africa\\/Maputo\",\"Africa\\/Maseru\",\"Africa\\/Mbabane\",\"Africa\\/Mogadishu\",\"Africa\\/Monrovia\",\"Africa\\/Nairobi\",\"Africa\\/Ndjamena\",\"Africa\\/Niamey\",\"Africa\\/Nouakchott\",\"Africa\\/Ouagadougou\",\"Africa\\/Porto-Novo\",\"Africa\\/Sao_Tome\",\"Africa\\/Tripoli\",\"Africa\\/Tunis\",\"Africa\\/Windhoek\",\"America\\/Adak\",\"America\\/Anchorage\",\"America\\/Anguilla\",\"America\\/Antigua\",\"America\\/Araguaina\",\"America\\/Argentina\\/Buenos_Aires\",\"America\\/Argentina\\/Catamarca\",\"America\\/Argentina\\/Cordoba\",\"America\\/Argentina\\/Jujuy\",\"America\\/Argentina\\/La_Rioja\",\"America\\/Argentina\\/Mendoza\",\"America\\/Argentina\\/Rio_Gallegos\",\"America\\/Argentina\\/Salta\",\"America\\/Argentina\\/San_Juan\",\"America\\/Argentina\\/San_Luis\",\"America\\/Argentina\\/Tucuman\",\"America\\/Argentina\\/Ushuaia\",\"America\\/Aruba\",\"America\\/Asuncion\",\"America\\/Atikokan\",\"America\\/Bahia\",\"America\\/Bahia_Banderas\",\"America\\/Barbados\",\"America\\/Belem\",\"America\\/Belize\",\"America\\/Blanc-Sablon\",\"America\\/Boa_Vista\",\"America\\/Bogota\",\"America\\/Boise\",\"America\\/Cambridge_Bay\",\"America\\/Campo_Grande\",\"America\\/Cancun\",\"America\\/Caracas\",\"America\\/Cayenne\",\"America\\/Cayman\",\"America\\/Chicago\",\"America\\/Chihuahua\",\"America\\/Costa_Rica\",\"America\\/Creston\",\"America\\/Cuiaba\",\"America\\/Curacao\",\"America\\/Danmarkshavn\",\"America\\/Dawson\",\"America\\/Dawson_Creek\",\"America\\/Denver\",\"America\\/Detroit\",\"America\\/Dominica\",\"America\\/Edmonton\",\"America\\/Eirunepe\",\"America\\/El_Salvador\",\"America\\/Fort_Nelson\",\"America\\/Fortaleza\",\"America\\/Glace_Bay\",\"America\\/Goose_Bay\",\"America\\/Grand_Turk\",\"America\\/Grenada\",\"America\\/Guadeloupe\",\"America\\/Guatemala\",\"America\\/Guayaquil\",\"America\\/Guyana\",\"America\\/Halifax\",\"America\\/Havana\",\"America\\/Hermosillo\",\"America\\/Indiana\\/Indianapolis\",\"America\\/Indiana\\/Knox\",\"America\\/Indiana\\/Marengo\",\"America\\/Indiana\\/Petersburg\",\"America\\/Indiana\\/Tell_City\",\"America\\/Indiana\\/Vevay\",\"America\\/Indiana\\/Vincennes\",\"America\\/Indiana\\/Winamac\",\"America\\/Inuvik\",\"America\\/Iqaluit\",\"America\\/Jamaica\",\"America\\/Juneau\",\"America\\/Kentucky\\/Louisville\",\"America\\/Kentucky\\/Monticello\",\"America\\/Kralendijk\",\"America\\/La_Paz\",\"America\\/Lima\",\"America\\/Los_Angeles\",\"America\\/Lower_Princes\",\"America\\/Maceio\",\"America\\/Managua\",\"America\\/Manaus\",\"America\\/Marigot\",\"America\\/Martinique\",\"America\\/Matamoros\",\"America\\/Mazatlan\",\"America\\/Menominee\",\"America\\/Merida\",\"America\\/Metlakatla\",\"America\\/Mexico_City\",\"America\\/Miquelon\",\"America\\/Moncton\",\"America\\/Monterrey\",\"America\\/Montevideo\",\"America\\/Montserrat\",\"America\\/Nassau\",\"America\\/New_York\",\"America\\/Nipigon\",\"America\\/Nome\",\"America\\/Noronha\",\"America\\/North_Dakota\\/Beulah\",\"America\\/North_Dakota\\/Center\",\"America\\/North_Dakota\\/New_Salem\",\"America\\/Nuuk\",\"America\\/Ojinaga\",\"America\\/Panama\",\"America\\/Pangnirtung\",\"America\\/Paramaribo\",\"America\\/Phoenix\",\"America\\/Port-au-Prince\",\"America\\/Port_of_Spain\",\"America\\/Porto_Velho\",\"America\\/Puerto_Rico\",\"America\\/Punta_Arenas\",\"America\\/Rainy_River\",\"America\\/Rankin_Inlet\",\"America\\/Recife\",\"America\\/Regina\",\"America\\/Resolute\",\"America\\/Rio_Branco\",\"America\\/Santarem\",\"America\\/Santiago\",\"America\\/Santo_Domingo\",\"America\\/Sao_Paulo\",\"America\\/Scoresbysund\",\"America\\/Sitka\",\"America\\/St_Barthelemy\",\"America\\/St_Johns\",\"America\\/St_Kitts\",\"America\\/St_Lucia\",\"America\\/St_Thomas\",\"America\\/St_Vincent\",\"America\\/Swift_Current\",\"America\\/Tegucigalpa\",\"America\\/Thule\",\"America\\/Thunder_Bay\",\"America\\/Tijuana\",\"America\\/Toronto\",\"America\\/Tortola\",\"America\\/Vancouver\",\"America\\/Whitehorse\",\"America\\/Winnipeg\",\"America\\/Yakutat\",\"America\\/Yellowknife\",\"Antarctica\\/Casey\",\"Antarctica\\/Davis\",\"Antarctica\\/DumontDUrville\",\"Antarctica\\/Macquarie\",\"Antarctica\\/Mawson\",\"Antarctica\\/McMurdo\",\"Antarctica\\/Palmer\",\"Antarctica\\/Rothera\",\"Antarctica\\/Syowa\",\"Antarctica\\/Troll\",\"Antarctica\\/Vostok\",\"Arctic\\/Longyearbyen\",\"Asia\\/Aden\",\"Asia\\/Almaty\",\"Asia\\/Amman\",\"Asia\\/Anadyr\",\"Asia\\/Aqtau\",\"Asia\\/Aqtobe\",\"Asia\\/Ashgabat\",\"Asia\\/Atyrau\",\"Asia\\/Baghdad\",\"Asia\\/Bahrain\",\"Asia\\/Baku\",\"Asia\\/Bangkok\",\"Asia\\/Barnaul\",\"Asia\\/Beirut\",\"Asia\\/Bishkek\",\"Asia\\/Brunei\",\"Asia\\/Chita\",\"Asia\\/Choibalsan\",\"Asia\\/Colombo\",\"Asia\\/Damascus\",\"Asia\\/Dhaka\",\"Asia\\/Dili\",\"Asia\\/Dubai\",\"Asia\\/Dushanbe\",\"Asia\\/Famagusta\",\"Asia\\/Gaza\",\"Asia\\/Hebron\",\"Asia\\/Ho_Chi_Minh\",\"Asia\\/Hong_Kong\",\"Asia\\/Hovd\",\"Asia\\/Irkutsk\",\"Asia\\/Jakarta\",\"Asia\\/Jayapura\",\"Asia\\/Jerusalem\",\"Asia\\/Kabul\",\"Asia\\/Kamchatka\",\"Asia\\/Karachi\",\"Asia\\/Kathmandu\",\"Asia\\/Khandyga\",\"Asia\\/Kolkata\",\"Asia\\/Krasnoyarsk\",\"Asia\\/Kuala_Lumpur\",\"Asia\\/Kuching\",\"Asia\\/Kuwait\",\"Asia\\/Macau\",\"Asia\\/Magadan\",\"Asia\\/Makassar\",\"Asia\\/Manila\",\"Asia\\/Muscat\",\"Asia\\/Nicosia\",\"Asia\\/Novokuznetsk\",\"Asia\\/Novosibirsk\",\"Asia\\/Omsk\",\"Asia\\/Oral\",\"Asia\\/Phnom_Penh\",\"Asia\\/Pontianak\",\"Asia\\/Pyongyang\",\"Asia\\/Qatar\",\"Asia\\/Qostanay\",\"Asia\\/Qyzylorda\",\"Asia\\/Riyadh\",\"Asia\\/Sakhalin\",\"Asia\\/Samarkand\",\"Asia\\/Seoul\",\"Asia\\/Shanghai\",\"Asia\\/Singapore\",\"Asia\\/Srednekolymsk\",\"Asia\\/Taipei\",\"Asia\\/Tashkent\",\"Asia\\/Tbilisi\",\"Asia\\/Tehran\",\"Asia\\/Thimphu\",\"Asia\\/Tokyo\",\"Asia\\/Tomsk\",\"Asia\\/Ulaanbaatar\",\"Asia\\/Urumqi\",\"Asia\\/Ust-Nera\",\"Asia\\/Vientiane\",\"Asia\\/Vladivostok\",\"Asia\\/Yakutsk\",\"Asia\\/Yangon\",\"Asia\\/Yekaterinburg\",\"Asia\\/Yerevan\",\"Atlantic\\/Azores\",\"Atlantic\\/Bermuda\",\"Atlantic\\/Canary\",\"Atlantic\\/Cape_Verde\",\"Atlantic\\/Faroe\",\"Atlantic\\/Madeira\",\"Atlantic\\/Reykjavik\",\"Atlantic\\/South_Georgia\",\"Atlantic\\/St_Helena\",\"Atlantic\\/Stanley\",\"Australia\\/Adelaide\",\"Australia\\/Brisbane\",\"Australia\\/Broken_Hill\",\"Australia\\/Darwin\",\"Australia\\/Eucla\",\"Australia\\/Hobart\",\"Australia\\/Lindeman\",\"Australia\\/Lord_Howe\",\"Australia\\/Melbourne\",\"Australia\\/Perth\",\"Australia\\/Sydney\",\"Europe\\/Amsterdam\",\"Europe\\/Andorra\",\"Europe\\/Astrakhan\",\"Europe\\/Athens\",\"Europe\\/Belgrade\",\"Europe\\/Berlin\",\"Europe\\/Bratislava\",\"Europe\\/Brussels\",\"Europe\\/Bucharest\",\"Europe\\/Budapest\",\"Europe\\/Busingen\",\"Europe\\/Chisinau\",\"Europe\\/Copenhagen\",\"Europe\\/Dublin\",\"Europe\\/Gibraltar\",\"Europe\\/Guernsey\",\"Europe\\/Helsinki\",\"Europe\\/Isle_of_Man\",\"Europe\\/Istanbul\",\"Europe\\/Jersey\",\"Europe\\/Kaliningrad\",\"Europe\\/Kiev\",\"Europe\\/Kirov\",\"Europe\\/Lisbon\",\"Europe\\/Ljubljana\",\"Europe\\/London\",\"Europe\\/Luxembourg\",\"Europe\\/Madrid\",\"Europe\\/Malta\",\"Europe\\/Mariehamn\",\"Europe\\/Minsk\",\"Europe\\/Monaco\",\"Europe\\/Moscow\",\"Europe\\/Oslo\",\"Europe\\/Paris\",\"Europe\\/Podgorica\",\"Europe\\/Prague\",\"Europe\\/Riga\",\"Europe\\/Rome\",\"Europe\\/Samara\",\"Europe\\/San_Marino\",\"Europe\\/Sarajevo\",\"Europe\\/Saratov\",\"Europe\\/Simferopol\",\"Europe\\/Skopje\",\"Europe\\/Sofia\",\"Europe\\/Stockholm\",\"Europe\\/Tallinn\",\"Europe\\/Tirane\",\"Europe\\/Ulyanovsk\",\"Europe\\/Uzhgorod\",\"Europe\\/Vaduz\",\"Europe\\/Vatican\",\"Europe\\/Vienna\",\"Europe\\/Vilnius\",\"Europe\\/Volgograd\",\"Europe\\/Warsaw\",\"Europe\\/Zagreb\",\"Europe\\/Zaporozhye\",\"Europe\\/Zurich\",\"Indian\\/Antananarivo\",\"Indian\\/Chagos\",\"Indian\\/Christmas\",\"Indian\\/Cocos\",\"Indian\\/Comoro\",\"Indian\\/Kerguelen\",\"Indian\\/Mahe\",\"Indian\\/Maldives\",\"Indian\\/Mauritius\",\"Indian\\/Mayotte\",\"Indian\\/Reunion\",\"Pacific\\/Apia\",\"Pacific\\/Auckland\",\"Pacific\\/Bougainville\",\"Pacific\\/Chatham\",\"Pacific\\/Chuuk\",\"Pacific\\/Easter\",\"Pacific\\/Efate\",\"Pacific\\/Enderbury\",\"Pacific\\/Fakaofo\",\"Pacific\\/Fiji\",\"Pacific\\/Funafuti\",\"Pacific\\/Galapagos\",\"Pacific\\/Gambier\",\"Pacific\\/Guadalcanal\",\"Pacific\\/Guam\",\"Pacific\\/Honolulu\",\"Pacific\\/Kiritimati\",\"Pacific\\/Kosrae\",\"Pacific\\/Kwajalein\",\"Pacific\\/Majuro\",\"Pacific\\/Marquesas\",\"Pacific\\/Midway\",\"Pacific\\/Nauru\",\"Pacific\\/Niue\",\"Pacific\\/Norfolk\",\"Pacific\\/Noumea\",\"Pacific\\/Pago_Pago\",\"Pacific\\/Palau\",\"Pacific\\/Pitcairn\",\"Pacific\\/Pohnpei\",\"Pacific\\/Port_Moresby\",\"Pacific\\/Rarotonga\",\"Pacific\\/Saipan\",\"Pacific\\/Tahiti\",\"Pacific\\/Tarawa\",\"Pacific\\/Tongatapu\",\"Pacific\\/Wake\",\"Pacific\\/Wallis\",\"UTC\"]")
const localTimezones = this.$moment.tz.names()
for (const [index, timezone] of localTimezones.entries()) {
if (!prodTimezones.includes(timezone)) {
console.log(timezone)
}
}
And the attached missing timezones from PHP? There's a lot more, but here's some
There's a thing called tz, zoneinfo, or the Olson database. It's maintained by the Internet Assigned Numbers Authority
It names zones in Continent/City or sometimes Continent/State/City format, like Asia/Kolkata or America/Indiana/Knox. Each named zone contains rules for converting to and from UTC time to local time, including the correct handling of summer time.
The database contains the temporopolitical history of time zone and summer time changeovers for the city (and surrounding regions). So, if the government of, say, Knox Indiana USA, changes the summer time rules next year, their entry gets updated in a maintenance release of the database.
If Spain decides to repudiate its Franco-era decision to use the same time zone as 'Europe/Berlin', and move to the same zone as 'Europe/Lisbon', the updated zoneinfo data for 'Europe/Madrid' will reflect that change on its effective date. Updates to UNIX-based operating systems like Linux and MacOS include the most recent zoneinfo data.
php uses zoneinfo. So does MySql. moment.js adds synonyms to it. If somebody in Knox sets their time zone to moment's synonym 'US/Central' and the city council the changes the rules, they won't follow the change.
So, please consider using php's list in your application, because it's proven so far to be future-proof.
Per the PHP the docs, the full form of timezone_identifiers_list is:
timezone_identifiers_list ( int $timezoneGroup = DateTimeZone::ALL , string|null $countryCode = null ) : array
By default, the $timezoneGroup parameter is DateTimeZone::ALL, which is designed to return only the Zone entries from the IANA time zone database.
If you want to really return all valid time zone identifiers, then you need to also consider the Link entries in the IANA time zone database, which are established for backwards compatibility purposes. In PHP, you can pass the DateTimeZone::ALL_WITH_BC constant to get the entire list of Zone and Link identifiers.
// get all time zone identifiers, including backwards compatible.
$timezone_identifiers = timezone_identifiers_list(DateTimeZone::ALL_WITH_BC);
Or using the object-oriented style DateTimeZone::listIdentifiers:
// get all time zone identifiers, including backwards compatible.
$timezone_identifiers = DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC);
The list should mostly align with the one you get from Moment-Timezone.
I'll also point out that if at all possible you should consider using a different library than Moment and/or Moment-Timezone. Please read Moment's Project Status Page for more details. Thanks.

PHP - set default timezone offset for areas without timezone name (sea locations)

I am writing PHP app that uses timezones. For land based GPS, I use
date_default_timezone_set();
with correct name like Europe/Dublin. However, if the GPS location is not land but it is on the sea, there is no timezone name, but time is offseted from UTC. The calculation is aprox. 1 hour for 15 degrees of longitude. I have calculated UTC offset manually, but I am not able to set it to PHP. I am using date method to print human-readable dates from unix timestamps.
I have found that there are Others timezones names (https://www.php.net/manual/en/timezones.others.php) like Etc/GMT+10, but the PHP documentation states this:
Warning Please do not use any of the timezones listed here (besides UTC), they
only exist for backward compatible reasons, and may expose erroneous
behavior.
Is it safe to use the Etc-based offsets? Or is there any other safe solution? I have not found any. In date_default_timezone_set() comments someone "uses" this: https://www.php.net/manual/en/function.date-default-timezone-set.php#96551 but it seems very ugly.
I am using PHP 7.1.
The time zones in PHP originate from the IANA tz database.
In particular, zones like Etc/GMT+10 originate from the etcetera file in the tz data, which says (emphasis mine):
... These days, the
tz files cover almost all the inhabited world, and the only practical
need now for the entries that are not on UTC are for ships at sea
that cannot use POSIX TZ settings.
Thus, you can indeed and should use them for the purpose you described.
Keep in mind that they only exist for whole-hour offsets from Etc/GMT-14 to Etc/GMT+12, and that the sign in the zone name is opposite from standard conventions. In other words Etc/GMT+10 is actually UTC-10.
One other note. My understanding is that it is common for ships at sea to use a land-based time zone when they are in the territorial waters near such lands, including any daylight saving time adjustment in effect. For example, a ship traveling within the territorial waters of the USA along the Pacific coast should change their clocks to use PST (UTC-8) in the winter and PDT (UTC-7) in the summer. This is modeled by America/Los_Angeles, whereas a pure longitudinal calculation would use UTC-8 year-round (Etc/GMT+8).
All of this is flexible, as the captain of the ship can basically decide the shipboard time at their discretion. See Wikipedia's article on nautical time for more specifics.
You might also want to read here about methods to use GPS coordinates to resolve an IANA time zone. Those that use the Time Zone Boundary Builder project as a data source (such as the Geo-Timezone PHP library) benefit from both nautical time and territorial waters being taken into consideration.

Get the official American time zone in PHP?

I have account stored in my database and I have each account state on file, from there I want to link each account to a timezone based on the account state.
so if the account's state is California then the timezone will be "America/Los_Angeles"
More, I want to break down the time zone that is provided at this page http://php.net/manual/en/timezones.america.php by
Atlantic
Eastern
Central
Mountain
Pacific
Alaska
Hawaii - Aleutian
Now I will be able to sort my data based on 7 time zones and also I can have all my account linked to a timezone so I can determine their time zone.
So My question
1) How can I figure out what state is linked to what timezone. (example: California = "America/Los_Angeles")
2) Which timezone are linked to what timezone category. (example: California = Pacific)
You haven't thought this through.
Many US states have multiple time zones. For example, South Dakota has both Mountain and Central time zones.
If you desire to resolve a location to a time zone, you will need a much more granular location. Ideally, a latitude and longitude. If you don't have one, you can approximate the centroid lat/lon of a zip code, and then use that against any of many various services or databases that will resolve that to a time zone. But be very careful, not all zip codes represent physical locations, and zip codes change frequently.
It's always best to go to the source:
http://www.php.net/manual/en/timezones.america.php
For all timezones:
http://www.php.net/manual/en/timezones.php
If you read down the page on the first link, someone was nice enough to make an array if you need the abbreviations:
$aTimeZones = array(
'America/Puerto_Rico'=>'AST',
'America/New_York'=>'EDT',
'America/Chicago'=>'CDT',
'America/Boise'=>'MDT',
'America/Phoenix'=>'MST',
'America/Los_Angeles'=>'PDT',
'America/Juneau'=>'AKDT',
'Pacific/Honolulu'=>'HST',
'Pacific/Guam'=>'ChST',
'Pacific/Samoa'=>'SST',
'Pacific/Wake'=>'WAKT',
);

Is "Timestamp Based Geolocation" possible and have an accuracy in detecting location/cities?

I know the answer is possible but the accuracy is the main question.Any idea?
Well the world is divided into about 24 timezones (there's a few odd quirks with 1/2 hr timezones as well) and two hemispheres, where daylight savings are reversed (Summer in the southern hemisphere is Winter in the Northern), so identifying daylight savings, you can effectively divide the world into 48 time segments (not all countries use daylight savings, so it's not perfect, but gives you a rough idea). Of course, you'll need a little bit of javascript to read these settings from the user's PC.
The world surface is 510,072,000 km2, so by using timestamp based locations, you can narrow a user down to an area of about 10,626,500km2. As a comparison Canada (the second largest country in the world) has an area of 9,984,670km2. Mainland USA is split across 5 timezones (including Alaska), so any American user would be limited to an accuracy of 5 cities across the entire country... you can't tell the difference between New York and Miami, or between San Francisco and Seattle. Using timezones, ou couldn't tell if I was in London, Lisbon or Dakar.
If that's an accurate enough guess for you, then all well and good. But GeoIP location gives you a far better accuracy, and libraries already exist to use that method... so why not use it?
Well, with user's local time you can only get his local time:). Really, there isn't any way to determine even their country that way. If you detect, say, UTC+1 - there's no way to find out even if it's Europe or Africa. You get much more accurate results analysing client's IP (See Geo IP Location book) and/or $_SERVER['HTTP_ACCEPT_LANGUAGE'].

convert timezone identifier to name? (Asia/Calcutta to Indian Standard Time)

I am working on a php (codeigniter) project. I have been doing a lot of work in timezones to convert times between different timezones and the server timezone.
I know how to convert a php timezone identifier to its abbreviation (like Asia/Calcutta to IST). But now, I need the expansion of this abbreviation (Indian Standard Time).
Is there any way to do this in php code, or any webservice to convert it? This is my last step in complete mastery in timezone programming! :)
Edit: As one person who answered pointed out, abbreviations can be ambiguous.. But timezone identifiers aren't. So can I directly convert "Asia/Calcutta" to "Indian Standard Time"? There is no ambiguity in that..
You general, you can't do it because it's ambiguous -- the same abbreviation is used for differente timezones.
Example:
CDT: Central Daylight Time (America/Cancun, America/Chicago, ...)
CDT: Mexico Central Daylight Time (America)
CDT: Cuba Central Daylight Time (America)
CDT: Canada Central Daylight Time (America)
Even IST is not unique:
IST: Irish Summer Time (Europe)
IST: Indian Standard Time (Asia)
IST: Israel Standard Time (not in zic)
There are, however, several heuristics. For instance, PostgreSQL comes with several timezone sets that you can swap depending on the continent of your target audience.
#Munim: Ahh... I didn't know about this. If that is the case, do you
know how to convert a timezone identifier like Asia/Calcutta to Indian
standard time? No ambiguity there..
Yup, (almost) no ambiguity at all. Almost because a timezone identifier might also map to several abbreviations depending on daylight savings and political changes. In PHP, you can do that with either DateTimeZone::listAbbreviations() (but this one isn't very reliable since it shows all the abbreviations used since the dawn of tzdb) or DateTimeZone::getTransitions() - this one is better since if you traverse the array backwards and return the first transition abbr that isn't observing DST.
There is no one-to-one mapping between timezone abbreviations, timezone offsets and timezone expansions. For example, CDT stands for Central Daylight Time which maps to both UTC+10:30 (Australia) and UTC-5 (North America). It has the same abbreviation, but maps to different continents and time offsets.
Similarly, CST is Central Standard Time and Central Summer Time and maps onto the following 3 offsets: UTC+9:30, UTC-6 and UTC+10:30
IST, which you refer to above, is also Irish Summer Time and maps to UTC+1
So, to answer your question, nope, you can't do it. If you really want to achieve mastery in timezone programming, read up on the ISO specifications for the same.
Maye you should look this php function timezone_name_from_abbr()

Categories