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?
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 forAPT.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.]
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.
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)
.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:
This was exactly the data I needed to be able to make my function:
The data is available through the following GET request:
[appId]
and[appKey]
will be provided after creating a free flightstats.com developer account here: https://developer.flightstats.com/signupYou 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 ofAmerica/Los_Angeles
. These time zone codes are understood by the PHP date/time API, and many other systems.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.
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.