PHP 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 'UTC' for '8.0/no DST' instead in /path/to/file.php on line 17
PHP 5.1+ 強制要求顯式設置時區。未設置時,PHP 會嘗試讀取系統時區,失敗則回退到 UTC 並拋出警告。系統時區與應用期望時區(如 Asia/Shanghai)不一致,會導致 date()、strtotime() 等函數輸出偏差(相差 8 小時)。
在腳本入口(如 index.php、框架啟動文件)最早位置調用:
<?php
date_default_timezone_set('Asia/Shanghai'); // 或 'PRC'(中國標準時間別名)
// 後續所有 date/time 函數自動使用此時區
優點: 無需修改 php.ini,跨環境部署一致,優先級高於配置文件。
php.ini:php --ini 或 phpinfo() 查看 Loaded Configuration File。date.timezone,取消註釋並設置:date.timezone = Asia/Shanghai
| 地區 | 標識符 | 備註 |
|---|---|---|
| 中國大陸 | Asia/Shanghai | 推薦,IANA 標準 |
| 中國大陸 | PRC | 別名,等同 Asia/Shanghai |
| 台灣 | Asia/Taipei | |
| 香港 | Asia/Hong_Kong | |
| 新加坡 | Asia/Singapore | |
| UTC | UTC | 國際標準時間 |
完整列表見: PHP 官方時區列表
<?php
echo '當前時區: ' . date_default_timezone_get() . '<br>';
echo '當前時間: ' . date('Y-m-d H:i:s') . '<br>';
echo '時間戳: ' . time();
輸出應顯示 Asia/Shanghai 且時間與本地一致。
config/app.php 中 'timezone' => 'Asia/Shanghai'config/packages/framework.yaml framework: php_ini: date.timezone: 'Asia/Shanghai'config/app.php 'default_timezone' => 'Asia/Shanghai'date_default_timezone_set('Asia/Shanghai'); 或配置 components.formatter.timeZone