【发布时间】:2019-07-24 13:55:29
【问题描述】:
这对吗?
是否有一些内置的 PHP 方法可以做到这一点,或者我应该使用一个库来代替这个自定义函数?
/**
* Supports the 4 Generalized Time Zones shown here: https://www.timeanddate.com/time/zone/usa of the contiguous U.S. (https://en.wikipedia.org/w/index.php?title=Time_in_the_United_States&oldid=885295732#Zones_used_in_the_contiguous_U.S.)
* @see https://en.wikipedia.org/wiki/Tz_database
*
* @param string $ianatz
* @return string
* @throws \Exception
*/
public static function getUsaGeneralizedTimeZoneAbbrev($ianatz) {
if ($ianatz == 'America/New_York') {
return 'ET';
} else if ($ianatz == 'America/Chicago') {
return 'CT';
} else if ($ianatz == 'America/Phoenix') {
return 'MT';
} else if ($ianatz == 'America/Los_Angeles') {
return 'PT';
} else {
throw new \Exception('Please use one of the 4 supported time zones of the contiguous USA.');
}
}
注意:我对 EDT 和 EST 不感兴趣。我想要 generalized 版本 (ET),这样季节的变化不会使标签不正确(例如,对于可以接受任何季节日期的表单输入)。
【问题讨论】:
-
有理由不使用
$ianatz中提供的值吗? -
@GlenSolsberry 我想要简短的缩写,例如“ET”,而不是笨重的长“America/New_York”形式。