gpt4 book ai didi

php - date_default_timezone_set 不起作用,为什么?

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

date_default_timezone_set 不起作用。

我的代码:

ini_set('display_errors', true);
error_reporting(E_ALL);

date_default_timezone_set("UTC");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";
date_default_timezone_set("Asia/Shanghai");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";
ini_set("date.timezone","UTC");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";
ini_set("date.timezone","Asia/Shanghai");
echo date('Y-m-d H:i:s T') . "<br>";
echo date('Y-m-d H:i:s T', time()) . "<br>";

他们都返回相同的日期“2017-05-26 12:47:08 CST”,为什么?


更新:

我已经修复了这个问题,原因是我在CentOS7上使用了错误的方式更改时区:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

这种方式在 CentOS6 上是正确的,但在 CentOS7 中/etc/localtime 链接到/usr/share/zoneinfo/Etc/UTC,所以我损坏了 UTC 时区。

在 CentOS7 上更改时区的正确方法是:

timedatectl set-timezone "Asia/Shanghai"

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

所以我将/usr/share/zoneinfo/Etc/UTC 从其他系统复制到我的系统以解决此问题。

最佳答案

试试这个。

<?php
$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $now->format('Y-m-d H:i:s T');
?>

time() 与时区无关。这意味着,无论时区如何配置,它将始终以秒为单位返回自 1970 年 1 月 1 日以来的时间。它总是需要 UTC 时间。'

date_default_timezone_set(); NOT working

也检查一下 http://php.net/manual/pl/function.time.php#100220

关于php - date_default_timezone_set 不起作用,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44193842/

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