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

前端 未结 2 1945
独厮守ぢ
独厮守ぢ 2021-01-23 04:51

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 Ea

相关标签:
2条回答
  • 2021-01-23 05:24

    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'); 
    
    ?>
    
    0 讨论(0)
  • 2021-01-23 05:47

    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.

    0 讨论(0)
提交回复
热议问题