PHP:如何从时区名称(例如America / New_York)中获取时区值(例如:东部标准时间)? [英] PHP: How to get timezone value (ex: Eastern Standard Time) from timezone name (ex: America/New_York)?

查看:344
本文介绍了PHP:如何从时区名称(例如America / New_York)中获取时区值(例如:东部标准时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何地方都有PHP函数可以在时区名称之间进行转换(例如在此处找到的名称): http://php.net/manual/en/timezones.america.php )和标准时间(例如东部标准时间还是太平洋夏令时间)?

Is there a PHP function anywhere which converts between the timezone name (such as those found here: http://php.net/manual/en/timezones.america.php) and the "value" such as Eastern Standard Time, or Pacific Daylight Time?

不希望在区域之间进行转换,只是给定America / New_York(或其他)名称来获得EST,PDT等名称。 我发现的唯一类似问题是另一种语言。

Not looking to convert between zones, just get the EST, PDT, etc. names given the America/New_York (or other) name. The only similar question I found is for a different language.

推荐答案

如果您安装 PHP国际化包,您可以执行以下操作:

If you you install the PHP Internationalization Package, you can do the following:

IntlTimeZone::createTimeZone('America/New_York')->getDisplayName()

默认情况下,这将返回 CLDR 英文标准格式,在这种情况下为东部标准时间 。您可以在此处找到可用的其他选项。例如:

This will return the CLDR English standard-long form by default, which is "Eastern Standard Time" in this case. You can find the other options available here. For example:

IntlTimeZone::createTimeZone('Europe/Paris')->getDisplayName(true, IntlTimeZone::DISPLAY_LONG, 'fr_FR')

以上内容将返回欧洲中央大学(Heureavancéed'Europe centrale) ,这是中欧夏令时的法语。

The above will return "heure avancée d’Europe centrale" which is French for Central European Summer Time.

请注意,将第一个参数传递为 true 如果有关日期和时间的夏令时有效,否则为 false 。这可以通过以下技术进行说明:

Be careful to pass the first parameter as true if DST is in effect for the date and time in question, or false otherwise. This is illustrated by the following technique:

$tz = 'America/New_York';
$dt = new DateTime('2016-01-01 00:00:00', new DateTimeZone($tz));
$dst = $dt->format('I');
$text = IntlTimeZone::createTimeZone($tz)->getDisplayName($dst);
echo($text); // "Eastern Standard Time"

在此处使用PHP小提琴

请注意,这些字符串旨在显示给最终用户。如果您打算将它们用于某些编程目的(例如调用另一个API),那么它们就不合适-即使某些字符串的英文版碰巧对齐也是如此。例如,如果您将时区发送到Windows或.NET API或Ruby on Rails API,则这些字符串将不起作用。

Please note that these strings are intended for display to an end user. If your intent is to use them for some programmatically purpose, such as calling into another API, then they are not appropriate - even if the English versions of some of the strings happen to align. For example, if you are sending the time zone to a Windows or .NET API, or to a Ruby on Rails API, these strings will not work.

这篇关于PHP:如何从时区名称(例如America / New_York)中获取时区值(例如:东部标准时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆