Linux 技术手札

PHP 修改 timezone 时区设定

在 PHP 升级到 5.3 后, 在使用日期相关的 functions 时会出现类似以下的错误讯息:

Warning: strtotime() [function.strtotime]: 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 ‘Europe/Helsinki’ for ‘EEST/3.0/DST’ instead in /Path_to_your/www/wp-includes/functions.php on line 35

Warning: date() [function.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 ‘Europe/Helsinki’ for ‘EEST/3.0/DST’ instead in /Path_to_your/www/wp-includes/functions.php on line 43

解决方法分别可以透过修改 php.ini 或 php 程式码实现:
1. 修改 php.ini
在 php.ini 加入一行:
date.timezone = “Asia/Hong_Kong”

储存后需要重新启动 web server.

2. 修改 php 程式码
在 php 程式的最开諯加入以下一行语法:

date_default_timezone_set(‘Asia/Hong_Kong’);

上面所使用的 “Asia/Hong_Kong” 代表香港的时区, 其他时区可以到 PHP Timezone 寻找。

Exit mobile version