5

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.

Community
  • 1
  • 1
Bing
  • 3,071
  • 6
  • 42
  • 81
  • For what purpose? In what language? Please elaborate. – Matt Johnson-Pint May 10 '16 at 20:35
  • Why does the purpose matter? The language is PHP, hence the title, the first paragraph and the tag. – Bing May 10 '16 at 21:09
  • I meant, are you displaying this to a user? In English? Or are you passing the value into another API? – Matt Johnson-Pint May 10 '16 at 22:03
  • Ahh, sorry, I misunderstood. I am using a text-to-speech application to convey the time of an event, including the timezone in which it is happening. Yes, it is English-only at the moment, though your answer seems able to satisfy that condition, thank you. – Bing May 10 '16 at 23:03
  • Yes, that makes sense. You can use the solution I gave below, and pass a locale code for the language that matches your TTS engine. – Matt Johnson-Pint May 11 '16 at 01:44

2 Answers2

5

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.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
4

If you know the value from your list at (http://php.net/manual/en/timezones.america.php) you can do something like.

<?php

$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone('America/New_York'));
echo $dateTime->format('T'); 

?>
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • 1
    This is ALMOST what I was asking for, since T gives the abbreviations, but not it written out. I don't see the long-form though. (Also, it looks like this was found here, for anyone looking: http://php.net/manual/en/function.timezone-abbreviations-list.php) – Bing May 10 '16 at 21:34