gpt4 book ai didi

php - 更改 gmdate PHP 函数的时区

转载 作者:行者123 更新时间:2023-12-02 06:36:38 25 4
gpt4 key购买 nike

我正在使用 gmdate PHP 函数将当前日期插入数据库,但它显示的是 GMT 时间,我可以将其更改为我的时区 +3 吗?

这是代码

$sql = "INSERT INTO Students
VALUES ('','" . $info[$i]['firstname'] . "', '" . $info[$i]['lastname'] . "',
'" . $info[$i]['sex'] . "', '" . $info[$i]['major'] . "',
'" . $info[$i]['favorite'] . "', '" . $info[$i]['GPA'] . "',
'". gmdate('m/d/Y g:i:s A') . "')" ;

最佳答案

“+3”不是“时区”。这是与 UTC/GMT 的偏移量,可能会根据 DST 设置在全年发生变化。 时区类似于“欧洲/柏林”或“亚洲/东京”。

一旦你决定了一个真正的时区:

date_default_timezone_set('Europe/Berlin');
echo date('m/d/Y g:i:s A');

$date = new DateTime('now', new DateTimeZone('Europe/Berlin'));
echo $date->format('m/d/Y g:i:s A');

如果您只需要使用“+3”,那么 a)您就完蛋了,b)您可以简单地将“3 小时”添加到任何时间戳:

date_default_timezone_set('UTC');

echo date('m/d/Y g:i:s A', strtotime("$offset hours"));

echo date('m/d/Y g:i:s A', time() + ($offset * 60 * 60));

$date = new DateTime;
$date->modify("$offset hours");
echo $date->format('m/d/Y g:i:s A');

关于php - 更改 gmdate PHP 函数的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com