【问题标题】:Convert timezone in php在php中转换时区
【发布时间】:2012-03-23 07:01:08
【问题描述】:

我有这些时区。我想获取当前日期时间取决于给定的时区。用户将选择任一时区。所以需要返回当前时间。

问询
CDT
美国东部时间
高铁
MDT
MST
光动力法

如何转换?请帮忙

【问题讨论】:

    标签: php


    【解决方案1】:

    DateTime::setTimezone 会帮助你。

        <?php
        $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
        echo $date->format('Y-m-d H:i:sP') . "\n";
    
        $date->setTimezone(new DateTimeZone('Pacific/Chatham'));
        echo $date->format('Y-m-d H:i:sP') . "\n";
         ?>
    

    【讨论】:

      【解决方案2】:

      使用DateTime Class

      $time = time(); //Get the current time
      $date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
      echo $date->format('Y-m-d H:i:sP') . "\n"; //display date
      
      $date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
      echo $date->format('Y-m-d H:i:sP') . "\n"; //display data again
      

      这样,您不必像 mishu 的回答那样每次都提供与新参数相同的时间戳。

      【讨论】:

      • 但我只有缩写(ASKT,...)
      【解决方案3】:

      请参阅“the_dark_lord12001 at yahoo dot com”编写的课程,这将帮助您从缩写中获取时区,然后您可以使用它 使用 date_default_timezone_set 或 DateTime 类

      http://www.php.net/manual/en/datetimezone.listabbreviations.php

      希望对您有所帮助。

      ~K

      【讨论】:

        猜你喜欢
        • 2011-01-31
        • 2013-12-20
        • 1970-01-01
        • 2011-07-27
        • 2020-08-04
        • 2020-03-05
        • 2013-08-06
        相关资源
        最近更新 更多