Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to get timezone value (ex: Eastern Standard Time) from timezone name (ex: America/New_York)?

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?

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.

like image 408
Bing Avatar asked Feb 08 '23 01:02

Bing


1 Answers

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

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

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')

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

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"

Working PHP Fiddle Here

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.

like image 192
Matt Johnson-Pint Avatar answered Feb 10 '23 10:02

Matt Johnson-Pint