dongzhina7098 2015-06-19 18:02
浏览 72
已采纳

在PHP中获取更长的时区名称

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"?

  • 写回答

2条回答 默认 最新

  • duan0403788996 2015-06-20 21:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改