警告:date():依赖系统时区设置不安全。您必须使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了这些方法但仍然收到此警告,很可能是时区标识符拼写错误。我们暂时选择了时区 'UTC',但请设置 date.timezone 来选择您的时区。位于 /var/www/app/src/Utils/DateHelper.php 第 10 行
Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/app/src/Utils/DateHelper.php on line 10
ID: php/date-timezone-ini-error
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| PHP 5.6 | active | — | — | — |
| PHP 7.0 | active | — | — | — |
| PHP 7.4 | active | — | — | — |
| PHP 8.0 | active | — | — | — |
| PHP 8.1 | active | — | — | — |
| PHP 8.2 | active | — | — | — |
| PHP 8.3 | active | — | — | — |
根因分析
PHP 时区未在 php.ini(date.timezone)中配置,也未通过 date_default_timezone_set() 设置,导致 PHP 回退到系统猜测,这可能不可靠。
English
The PHP timezone is not configured in php.ini (date.timezone) or via date_default_timezone_set(), causing PHP to fall back to a system guess which may be unreliable.
官方文档
https://www.php.net/manual/en/function.date-default-timezone-set.php解决方案
-
在 php.ini 中设置时区:`date.timezone = 'Asia/Shanghai'`(替换为您的时区)。重启 Web 服务器或 PHP-FPM。
-
在 PHP 脚本顶部或引导文件(如 index.php 或 config.php)中添加 `date_default_timezone_set('Asia/Shanghai');`。 -
对于 CLI 脚本,在运行脚本之前通过环境变量设置时区:`export TZ='Asia/Shanghai'`。
无效尝试
常见但无效的做法:
-
70% 失败
The .user.ini file is ignored in module mode; the setting must be in the main php.ini.
-
80% 失败
PHP validates the timezone string; a typo results in fallback behavior.
-
65% 失败
The setting may not propagate to all PHP contexts (e.g., CLI or subrequests).