convert timezone identifier to name? (Asia/Calcutt

2019-08-10 17:06发布

I am working on a php (codeigniter) project. I have been doing a lot of work in timezones to convert times between different timezones and the server timezone.

I know how to convert a php timezone identifier to its abbreviation (like Asia/Calcutta to IST). But now, I need the expansion of this abbreviation (Indian Standard Time).

Is there any way to do this in php code, or any webservice to convert it? This is my last step in complete mastery in timezone programming! :)

Edit: As one person who answered pointed out, abbreviations can be ambiguous.. But timezone identifiers aren't. So can I directly convert "Asia/Calcutta" to "Indian Standard Time"? There is no ambiguity in that..

标签: php timezone
4条回答
Viruses.
2楼-- · 2019-08-10 17:23

@Munim: Ahh... I didn't know about this. If that is the case, do you know how to convert a timezone identifier like Asia/Calcutta to Indian standard time? No ambiguity there..

Yup, (almost) no ambiguity at all. Almost because a timezone identifier might also map to several abbreviations depending on daylight savings and political changes. In PHP, you can do that with either DateTimeZone::listAbbreviations() (but this one isn't very reliable since it shows all the abbreviations used since the dawn of tzdb) or DateTimeZone::getTransitions() - this one is better since if you traverse the array backwards and return the first transition abbr that isn't observing DST.

查看更多
\"骚年 ilove
3楼-- · 2019-08-10 17:27

There is no one-to-one mapping between timezone abbreviations, timezone offsets and timezone expansions. For example, CDT stands for Central Daylight Time which maps to both UTC+10:30 (Australia) and UTC-5 (North America). It has the same abbreviation, but maps to different continents and time offsets.

Similarly, CST is Central Standard Time and Central Summer Time and maps onto the following 3 offsets: UTC+9:30, UTC-6 and UTC+10:30

IST, which you refer to above, is also Irish Summer Time and maps to UTC+1

So, to answer your question, nope, you can't do it. If you really want to achieve mastery in timezone programming, read up on the ISO specifications for the same.

查看更多
贪生不怕死
4楼-- · 2019-08-10 17:29

Maye you should look this php function timezone_name_from_abbr()

查看更多
我想做一个坏孩纸
5楼-- · 2019-08-10 17:36

You general, you can't do it because it's ambiguous -- the same abbreviation is used for differente timezones.

Example:

  • CDT: Central Daylight Time (America/Cancun, America/Chicago, ...)
  • CDT: Mexico Central Daylight Time (America)
  • CDT: Cuba Central Daylight Time (America)
  • CDT: Canada Central Daylight Time (America)

Even IST is not unique:

  • IST: Irish Summer Time (Europe)
  • IST: Indian Standard Time (Asia)
  • IST: Israel Standard Time (not in zic)

There are, however, several heuristics. For instance, PostgreSQL comes with several timezone sets that you can swap depending on the continent of your target audience.

查看更多
登录 后发表回答