【问题标题】:How do I return the current time in PHP for the PST timezone?如何在 PHP 中返回 PST 时区的当前时间?
【发布时间】:2014-07-27 22:08:58
【问题描述】:

我必须为我的朋友设置一个时间命令(使用 twitch 的夜间机器人),但不知道如何打印当前时间。

我试过用这个:

<?php
$date = date_create(null, timezone_open('America/Los_Angeles'));
$tz = date_timezone_get($date);
echo date(" H:i", time());
?>

然而,这会返回当前服务器时间(我相当确定)而不是我选择的时区。

我使用了几种不同的东西,但它们都不起作用。它们要么什么都不显示,要么显示错误的时间。

【问题讨论】:

    标签: php


    【解决方案1】:

    尝试使用 DateTime() 类 - http://php.net/manual/en/datetime.construct.php

    $date = new DateTime(null, new DateTimeZone('America/Los_Angeles'));
    echo $date->format('H:i');
    

    注意null - 来自文档 :: Enter NULL here to obtain the current time when using the $timezone parameter.

    【讨论】:

      【解决方案2】:

      您使用的是 PHP 5.2 或更高版本吗?您可以使用 DateTime 类来帮助您。这个对另一个相关问题的回答可能会有所帮助:https://stackoverflow.com/a/14595648/1153203

      这是来自链接答案的 DateTime 示例:

      $datetime = new DateTime($dbTimestamp, $timezone);
      echo $datetime->format('Y-m-d H:i:s');
      $datetime->setTimezone(new DateTimeZone('Pacific/Nauru'));
      echo $datetime->format('Y-m-d H:i:s');
      

      【讨论】:

        猜你喜欢
        • 2020-01-18
        • 1970-01-01
        • 1970-01-01
        • 2021-03-12
        • 2011-09-14
        • 2016-10-27
        • 2012-04-07
        • 2012-03-20
        • 2011-03-15
        相关资源
        最近更新 更多