2

I am doing an IP lookup to determine a "best guess" for timezone offset from UTC and the country/region information.

So, for instance, I have offset -3 and country Brazil.

I want to show BRT instead of a random timezone in that same offset, i.e. ART

Is there some built-in way in PHP to be able to set the appropriate timezone with this information?

If not, what would be the easiest way to do this? (Assume that I can't make any outside API calls)

If I do my own database table, how do I ensure it stays up to date?

Thanks!

user138821
  • 434
  • 8
  • 15
  • 1
    Do you think that timezones are changing all the time to where staying "up to date" is a major concern? – Mike Brant Jan 29 '13 at 18:32
  • Depends on your definition of "major concern", but yes, they change frequently enough that I'm concerned. Even in the United States, Indiana changed DST observation as recently as 2005. – user138821 Jan 29 '13 at 19:18

3 Answers3

2

You cannot resolve a timezone from an offset, or even an offset plus country. See the TimeZone != Offset section of the TimeZone tag wiki. The offsets have changed at many points in time, and they will inevitably change again.

You need more information, such as latitude/longitude. See this community wiki.

If you can ask your user for their timezone, please do. That's the best way. My favorite UI for this is a JavaScript map-based timezone picker, such as this one. You can make a best-guess default choice using jsTimeZoneDetect.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

You don't have to roll your own database, here's one from GeoNames. You can also get all the timezone information from PHP.

Community
  • 1
  • 1
Antony
  • 14,900
  • 10
  • 46
  • 74
  • Cool, thanks, this is good enough for my purposes. I visited here when researching the answer, but didn't want to use their API and didn't see the downloadable data earlier. – user138821 Jan 29 '13 at 19:22
1

This PHP function give back an array of all existing Timezones including Offset, not exactly what you are looking for but maybe it helps:

function time_zonelist(){
    $return = array();
    $timezone_identifiers_list = timezone_identifiers_list();
    foreach($timezone_identifiers_list as $timezone_identifier){
        $date_time_zone = new DateTimeZone($timezone_identifier);
        $date_time = new DateTime('now', $date_time_zone);
        $hours = floor($date_time_zone->getOffset($date_time) / 3600);
        $mins = floor(($date_time_zone->getOffset($date_time) - ($hours*3600)) / 60);
        $hours = 'GMT' . ($hours < 0 ? $hours : '+'.$hours);
        $mins = ($mins > 0 ? $mins : '0'.$mins);
        $text = str_replace("_"," ",$timezone_identifier);
        $return[$timezone_identifier] = $text.' ('.$hours.':'.$mins.')';
    }
    return $return;
}
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
adilbo
  • 910
  • 14
  • 22