Given a time zone abbreviation like "EST", I can get "America/New York" by using timezone_name_from_abbr
. Is there any function that returns something like "Eastern Standard Time"?
问题:
回答1:
In general, you should not trust the timezone_name_from_abbr
function, because it only returns the first entry that it finds with a matching abbreviation. Time zone abbreviations are not unique. For example, there are 5 different time zones that share the abbreviation "CST". See this list of abbreviations on Wikipedia for even more examples of ambiguity.
As a specific case for PHP, consider the abbreviation "IST". One might expect timezone_name_from_abbr("IST")
to return "Asia/Kolkata"
, representing Indian Standard Time. However, it actually returns "Asia/Jerusalem"
, representing Israel Standard Time. Both time zones use the IST abbreviation, but "Asia/Jerusalem"
comes first alphabetically.
With that in mind, you should now see why it would also not be practical for a function to return "Eastern Standard Time" given only "EST". While it might be possible for EST, it won't work in all cases. Also consider that "Eastern Standard Time" is in English, and there are other abbreviations and textual descriptions for different languages.
There is a project that contains the information you're talking about, which is from Unicode, and called the Common Locale Data Repository (or CLDR). Using CLDR data, you would be able to write functions that that accept a time zone identifier such as "America/New_York"
, and a language/locale code such as "en-US"
, and return the abbreviations "ET", "EST" and "EDT", and the names "Eastern Time", "Eastern Standard Time", and "Eastern Daylight Time". You would then need to decide which name and abbreviation to use, based on the context and/or specific time you were talking about.
I've written such a library for .NET, called TimeZoneNames. You would need something similar for PHP. I don't know of a particular one to recommend for PHP, but perhaps someone has written one, or perhaps you can create the functions you need by directly consuming the CLDR source data.
回答2:
This was something I was after for some time and seeing someone else is looking for the same functionality it was the last push needed :)
webit4me/timezone, is a PHP tool which can help to get the given timezone's name, location or offset.
The tool also provides a workaround for timezones with similar abbreviations.
i.e. In Matt's example of IST which we have 3 different timezones
- India Standard Time (UTC +5:30)
- Irish Standard Time (UTC +1:00)
- Israel Standard Time (UTC +2:00)
the default IST object will refer to the first occurrence and then we will have 2 more objects IST100 and IST200 to address Irish and Israel standard times. Please note the 100 and 200 suffixes are coming from the timezone's offsets e.g. UTC +1:30 would be 130.
Hope this package will be helpful, and please feel free to tag along and contribute adjustments if required to http://github.com/webit4me/timezone