13

I am trying to make a PHP function that returns the UTC timezone for a given airport code (IATA/FAA).

What the function should do is something like this:

echo getTimezoneFromAirportCode("CPH"); // +1
echo getTimezoneFromAirportCode("CXI"); // +14

To make this function I need a list of all aiport codes and their timezones.

By searching a bit I found this list: https://sourceforge.net/p/openflights/code/HEAD/tree/openflights/data/airports.dat?format=raw (Source: http://openflights.org/data.html)

After looking up a couple of airport codes in the list I found out that some of the data was incorrect. For instance it lists CXI to be in the UTC -12 timezone - which according to this page is incorrect.

Does any of you know a public list that provides the data needed to make the getTimezoneFromAirportCode function?

Casper André Casse
  • 573
  • 2
  • 8
  • 20
  • Openflights gets its data from [ourairports.com/data](http://ourairports.com/data/), so you might as well take it from there directly. – Matt Johnson-Pint Apr 21 '16 at 14:19
  • [This page](https://nfdc.faa.gov/nfdcApps/services/airportLookup/airportDisplay.jsp?airportId=bur) has timezone information but as people have said it is United States only and I don't know where the data comes from. Perhaps that will help someone. – Sam Hobbs Feb 26 '17 at 05:07

5 Answers5

22

Seeing as the question is still open I'd like to point to my very own timezone map files. For your particular purpose, the IATA tzmap seems perfect:

https://raw.github.com/hroptatyr/dateutils/tzmaps/iata.tzmap

Obviously, you'd have to snarf the offset in question (it depends on a date as it might change over time) from the zoneinfo files, e.g. with zdump(1).

hroptatyr
  • 4,702
  • 1
  • 35
  • 38
14

You are confusing a "time zone" with a "time zone offset". They are not the same thing. You can't just ask for the offset at a location - you also would need to know the specific time in question. It's invalid to ask "What's the time zone offset for LAX" - because in the winter it will be -8 while in the summer it will be -7.

You can ask "what is the offset at this location right now", but that might give you a different answer depending on when you ask. So the date and time have to be part of the input.

What you really need to know instead is that LAX corresponds to the IANA time zone of America/Los_Angeles. These time zone codes are understood by the PHP date/time API, and many other systems. (.NET Windows users can convert these to Microsoft Windows time zones using my TimeZoneConverter library.)

To do this, you need to take the latitude and longitude of each airport (which are available from OpenFlights and many other places), and use one of the methods described here to lookup the IANA time zone for those coordinates.

I've already written code for this in C#, which you can find here. The output is included in a .csv file, which you can parse and use in any language you like. It includes both IANA and Microsoft time zones.

Be sure to also read the timezone tag wiki, especially the parts on the IANA database, and the section titled "Time Zone != Offset".

Also note that while the lat/lon coordinates in the OpenFlights database are great, the time zone data in that file is not very accurate, so going through the process I described is important. When it comes to the specifics of time zones, you should only trust the IANA database and make sure you keep updated.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 1
    The [OpenFlights](https://openflights.org/data.html) airports.dat (which is a CSV file) also includes Time Zone, DST, and IANA time zone. I'm not sure about PHP, but there's a python library that keeps up with DST for various regions (zoneinfo) based on IANA time zone, so I'm assuming there's something similar for PHP. – claypooj Sep 03 '22 at 00:07
  • 1
    @claypooj - That's good to point out, but I believe the reason I chose OurAirports + IANA data was that OpenFlights data isn't updated very often. Indeed, the data file their main page links to is hosted on GitHub [here](https://github.com/jpatokal/openflights/tree/master/data) which shows it's three years old. Whereas [OurAirports](https://ourairports.com/data/) was just updated today. (Also, OpenFlights data was derived from OurAirports anyway.) – Matt Johnson-Pint Sep 03 '22 at 19:08
4

I've managed to find a solution to the issue. Through the flightstats.com API it is possible to get a free but limited access to a complete airport database: https://developer.flightstats.com/api-docs/airports/v1

The API returns all active/inactive airports in the following format:

{
   "fs": "LAX",
   "iata": "LAX",
   "icao": "KLAX",
   "faa": "LAX",
   "name": "Los Angeles International Airport",
   "street1": "One World Way",
   "street2": "",
   "city": "Los Angeles",
   "cityCode": "LAX",
   "stateCode": "CA",
   "postalCode": "90045-5803",
   "countryCode": "US",
   "countryName": "United States",
   "regionName": "North America",
   "timeZoneRegionName": "America/Los_Angeles",
   "weatherZone": "CAZ041",
   "localTime": "2014-06-20T06:00:50.439",
   "utcOffsetHours": -7,
   "latitude": 33.943399,
   "longitude": -118.408279,
   "elevationFeet": 126,
   "classification": 1,
   "active": true,
   "delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/LAX?codeType=fs",
   "weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/LAX?codeType=fs"
}

This was exactly the data I needed to be able to make my function:

echo getTimezoneFromAirportCode("LAX"); // -7

The data is available through the following GET request:

https://api.flightstats.com/flex/airports/rest/v1/json/all?appId=[appId]&appKey=[appKey]

[appId] and [appKey] will be provided after creating a free flightstats.com developer account here: https://developer.flightstats.com/signup

Casper André Casse
  • 573
  • 2
  • 8
  • 20
3

Hmmm..perhaps you can make a converter yourself - it should be simple enough with a small database table mapping airport codes to timezones.

I found the following link containing a CSV file of over 5,000 airport codes and their timezone relative to UTC.

http://openflights.org/data.html

You can import the CSV from that link into your own database and then have your code work around the timezones and airports in that table.

sergio
  • 5,210
  • 7
  • 24
  • 46
2

I would recommend the FAA's "NASR" database, which is a 56-day dataset provided by the FAA. The database is a set of raw text files using fixed-width ASCII columns. Format description files are provided that show what each column is.

One of the files in that dataset is APT.txt which contains a time zone field for each airport; see the format description for APT.txt to find the column offsets. It may sound intimidating but it's very easy to parse.

Note also that this is the "official" raw navigation data product offered by the FAA. It's "from the horse's mouth" so to speak.

[EDIT: This database only covers US airports; see comments below.]

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • 1
    As far as I can see the file `APT.txt` only includes airports in USA? I found `LAX` but couldn't find `CXI`. I'm pretty sure I can use the list although I haven't located any timezone information yet. But I still need a list to get the codes from outside USA. – Casper André Casse Dec 12 '13 at 20:48
  • Sorry, I should've mentioned that... yes, that's true, it only contains US airports. There is unfortunately no publicly available authoritative global navigation database. The DAFIF is published by the US Department of Defense and has authoritative global data, but it's no longer available publicly. If you need global data you'll have to find some non-authoritative database online (which may likely be based on the last published DAFIF version which is many years old) or scrape a website like www.airnav.com. – TypeIA Dec 12 '13 at 20:51
  • If this is for commercial use you can also purchase official up-to-date global databases from companies like Jeppesen, but this is quite pricey and wouldn't likely be suitable for a non-commercial project. – TypeIA Dec 12 '13 at 20:52
  • 2
    TypeIA, are you certain there is time zone information in there? I've read through the whole apt_rf.txt file, describing the format, and don't see anything at all about time zones. I've also searched the APT.txt file itself, and can't find anything that looks like a time zone (e.g. "America/Denver", which should be the timezone for DIA). – Joe Strout Jul 08 '14 at 14:15