How can I find country details from time zone using php.
3 Answers
You know user's time zone and want to know where he is from? You can't do that, because, obviously, there is (alot) more than one country within one time zone.
If you want to find out the location of a user, use his ip, or geolocation.

- 44,202
- 36
- 123
- 164
You can make a map from the timezone name to country. But that only works if the are using zt-type names, ie Europe/Paris, and not if they use Windows insane names like "Romance Standard Time", nor if they use abbreviations like CET or the offset like +01:00. as these are not country-unique.
And even if you are on Unix, and you want to figure out the timezone name, you are out of luck. You can't do that reliably. So your user might have to select the timezone manually.
And in that case he can just as well select his country manually in the first place.
In short: There are way better to figure out a users country name than via the timezone. Try to give some context to the question. Is this via the web? Using php indicates this, but then how the heck do you know the users timezone!? :-)

- 167,292
- 41
- 224
- 251
I want to provide an answer here as I don't believe the others quite answer the actual question. Firstly, I am assuming (which I concur the OP didn't provide) that the Timezone is a PHP formatted timezone such as Europe/Paris
.
Based on this answer (How to get ISO 3166-1 compatible country code?) and the $country_code_to_name
array in the answer, you can use the following code to easily answer (and prove that no timezone covers multiple countries) the requirements of working out which country a timezone belongs to. I do apprecite that timezone offsets apply to multiple countries and I am therefore making an assumption we're talking about specific PHP timezones.
This will return the ISO 2 letter code or simply use $name
instead of $code
.
$all = [];
$lookup = [];
// Timezones per country
foreach ($country_code_to_name as $code=>$name){
$dates = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY,$code);
foreach ($dates as $zone){
$all[$zone][] = $name;
$lookup[$zone] = $code;
}
}
// Do any timezones apply to multiple countries?
foreach ($all as $zone=>$countries){
echo $zone.'-'.count($countries).'<br>';
}
// Which country does America/Chicago belong to?
echo $lookup['America/Chicago'];

- 3,875
- 30
- 32