【问题标题】:Display Time in PST以 PST 显示时间
【发布时间】:2011-03-04 03:40:34
【问题描述】:

使用 PHP 以 PST(西海岸)时间显示当前时间的最简单方法是什么?

【问题讨论】:

    标签: php timezone


    【解决方案1】:

    嗯,最简单的可能是:

    date_default_timezone_set('America/Los_Angeles');
    echo date('Y-m-d');
    

    查看supported timezones,找到适合您需求的。

    【讨论】:

    • 这不是设置服务器时间吗?这不是他们要问的。
    【解决方案2】:

    .

    echo date('r');
    putenv('TZ=PST');
    echo date('r');  
    

    【讨论】:

      【解决方案3】:

      在时区之间转换日期/时间:

      include ("Date.php");
      $d = new Date("2010-06-21 10:59:27"); // initialize object
      $d->setTZByID("GMT"); // set local time zone
      $d->convertTZByID("PST"); // convert to foreign time zone
      echo $d->format("%A, %d %B %Y %T"); // retrieve converted date/time
      

      【讨论】:

        【解决方案4】:

        让我们尝试一个使用 PHP 的现代日期处理的解决方案。此示例需要 PHP 5.2 或更高版本。

        // Right now it's about four minutes before 1 PM, PST.
        $pst = new DateTimeZone('America/Los_Angeles');
        $three_hours_ago = new DateTime('-3 hours', $pst); // first argument uses strtotime parsing
        echo $three_hours_ago->format('Y-m-d H:i:s'); // "2010-06-15 09:56:36"
        

        【讨论】:

          【解决方案5】:

          如果您正在使用或有权访问 Carbon,您可以这样做:

          $timezone = 'America/Los_Angeles';
          $now = Carbon::now()->tz($timezone)->toDateTimeString();
          echo $now;
          

          【讨论】:

            猜你喜欢
            • 2013-06-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-14
            • 2021-09-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多