PHP date() with timezone?


PHP date() with timezone?

所以我检查了PHP中支持的时区列表,我想知道如何将它们包含在date()函数中?谢谢!

我不想要默认时区,每个用户的时区都存储在数据库中,我获取用户的时区并使用它。如何?不过,我知道如何从数据库中获取它,而不是如何使用它。

对于这样的任务,你应该真正使用 PHP 的 DateTime 类。请忽略所有建议您使用 date(( 或 date_set_time_zone 的答案,它简直糟糕且过时。

我将使用伪代码进行演示,因此请尝试调整代码以满足您的需求。

假设变量$tz包含有效时区的字符串名称,变量$timestamp包含您希望根据时区格式化的时间戳,则代码如下所示:

$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');

DateTime类功能强大,要掌握它的所有功能 - 您应该花一些时间阅读 php.net。要完全回答您的问题 - 是的,您可以动态调整时区参数(在每次迭代时,从数据库读取时,您可以创建一个新的 DateTimeZone(( 对象(。

如果我理解正确,你需要先设置时区,就像:

date_default_timezone_set('UTC');

并且比您可以使用日期功能:

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS 'of F Y h:i:s A');

上面的答案让我跳过了一些箍/陷阱,所以只是发布对我有用的更干净的代码:

$dt = new DateTime();
$dt->setTimezone(new DateTimeZone('America/New_York'));
$dt->setTimestamp(123456789);
echo $dt->format('F j, Y @ G:i');

请改用 DateTime 类,因为它支持时区。date() 的 DateTime 等效项是 DateTime::format

DateTime的一个非常有用的包装是Carbon-一定要看看。

您需要在数据库中存储为 UTC 并在应用程序级别进行转换。

它应该是这样的:

date_default_timezone_set('America/New_York');

你可以将时区差异添加到Unix时间戳。莫斯科示例 (UTC+3(

echo date('d.m.Y H:i:s', time() + 3 * 60 * 60);

根据其他答案,我构建了一行,我想您需要当前的日期时间。如果需要不同的时间戳,则很容易调整。

$dt = (new DateTime("now", new DateTimeZone('Europe/Rome')))->format('d-m-Y_His');

试试这个。您可以传递 unix 时间戳或日期时间字符串

public static function convertToTimezone($timestamp, $fromTimezone, $toTimezone, $format='Y-m-d H:i:s') 
    {
        $datetime = is_numeric($timestamp) ?
                    DateTime::createFromFormat ('U' , $timestamp, new DateTimeZone($fromTimezone)) :
                    new DateTime($timestamp, new DateTimeZone($fromTimezone));
        $datetime->setTimezone(new DateTimeZone($toTimezone));
        return $datetime->format($format);
    }

在2019年完美运行:

date('Y-m-d H:i:s',strtotime($date. ' '.$timezone)); 

我创建了这个非常简单的函数,它就像一个魅力:

function ts2time($timestamp,$timezone){ /* input: 1518404518,America/Los_Angeles */            
        $date = new DateTime(date("d F Y H:i:s",$timestamp));
        $date->setTimezone(new DateTimeZone($timezone));
        $rt=$date->format('M d, Y h:i:s a'); /* output: Feb 11, 2018 7:01:58 pm */
        return $rt;
    }

我已经尝试了基于日期时间类的答案。在它们工作时,我发现了一个更简单的解决方案,该解决方案使 DateTime 对象在创建时知道时区。

$dt = new DateTime("now", new DateTimeZone('Asia/Jakarta'));
echo $dt->format("Y-m-d H:i:s");

这将返回印度尼西亚雅加达的当前当地时间。

上面没有提到。您还可以通过在构造函数中提供带有前导 @ 符号的时间戳作为字符串来构建 DateTime 对象。

$dt = new DateTime('@123456789');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('F j, Y - G:i');

请参阅有关复合格式的文档:https://www.php.net/manual/en/datetime.formats.compound.php

如果您使用 Team EJ 的答案,则在 DateTime 的格式字符串中使用 T 将显示一个三个字母的缩写,但您可以像这样获取时区的长名称:

$date = new DateTime('2/3/2022 02:11:17');
$date->setTimezone(new DateTimeZone('America/Chicago'));
echo "'n" . $date->format('Y-m-d h:i:s T');
/* Displays 2022-02-03 02:11:17 CST "; */
$t = $date->getTimezone();
echo "'nTimezone: " . $t->getName();
/* Displays Timezone: America/Chicago */
$now = new DateTime();
$now->format('d-m-Y H:i:s T')

将输出:

29-12-2021 12:38:15 UTC

我在主机上遇到了一个奇怪的问题。当我使用以下代码检查时区时,时区设置正确。

echo ini_get('date.timezone');

但是,它返回的时间是UTC。

解决方案是使用以下代码,因为在 PHP 配置中正确设置了时区。

date_default_timezone_set(ini_get('date.timezone'));

您可以在date_default_timezone_set函数中替换数据库值, date_default_timezone_set(SOME_PHP_VARIABLE(;但只需要处理与时区相关的确切值。