无法在 PHP 中设置默认时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6156833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:31:01  来源:igfitidea点击:

Can't set default timezone in PHP

phptimezonephp-ini

提问by Stann

I've set this in my php.ini file, and restarted php5-fpm process

我已经在我的 php.ini 文件中设置了这个,并重新启动了 php5-fpm 进程

date.timezone = "UTC"

But phpinfo() script still shows this:

但是 phpinfo() 脚本仍然显示:

date.timezone   America/New_York    America/New_York

phpinfo()also shows that correct ini file is being used.

phpinfo()还显示正在使用正确的 ini 文件。

I've restarted - but problem persisted. Then I did:

我已经重新启动 - 但问题仍然存在。然后我做了:

sudo service nginx stop
sudo service apache2 start

and date.timezone is correct when mod-php (apache2) is in use instead of fcgi that is php in nginx.

当使用 mod-php (apache2) 而不是 nginx 中的 php fcgi 时,date.timezone 是正确的。

回答by KingCrunch

You probably edited the wrong php.ini. See php_info()(or php -ifor the cli interpreter) which one is used. For example on ubuntu (and probably other linux distributions) its /etc/php5/cli/php.inifor the cli-interpreter, /etc/php5/apache/php.ini/for the one used by Apaches mod_phpand /etc/php5/cgi/php.iniused by php5-cgi(which is used by nginx).

你可能编辑错了php.ini。查看php_info()(或php -i查看 cli 解释器)使用的是哪一个。例如,在 ubuntu(可能还有其他 linux 发行版)上,它/etc/php5/cli/php.ini用于 cli-interpreter,/etc/php5/apache/php.ini/用于 Apachesmod_php/etc/php5/cgi/php.iniused by php5-cgi(由 使用nginx)。

回答by thiru

The problem seems to be be with php-fpm processes that lingers around and refers to the old php.ini file settings. This worked for me:

问题似乎与 php-fpm 进程有关,这些进程一直存在并引用旧的 php.ini 文件设置。这对我有用:

Get the process ids for php-fpm

获取 php-fpm 的进程 ID

root@thiru:/etc/php5/fpm/conf.d# ps aux | head -1 && ps aux | grep php-fpm | grep -v grep
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     32650  0.0  0.5  86624 17032 ?        Ss   21:44   0:00    php-fpm: master process (/etc/php5/fpm/php-fpm.conf)                    
www-data 32652  0.0  0.1  86624  4700 ?        S    21:44   0:00 php-fpm: pool www                                                       
www-data 32653  0.0  0.1  86624  4704 ?        S    21:44   0:00 php-fpm: pool www   

Kill the processes. Starting with the master.

杀死进程。从大师开始。

kill -9 32650
kill -9 32652
kill -9 32653

Start php-fpm using the init script

使用 init 脚本启动 php-fpm

service php5-fpm start or /etc/init.d/php5-fpm start

回答by Dave Kiss

List of supported timezone values: http://php.net/timezones

支持的时区值列表:http: //php.net/timezones

回答by Octeny

Check your FPM Installation, if you have a pool with a active chroot!

检查您的 FPM 安装,如果您有一个带有活动 chroot 的池!

If you have a chroot environment, you need to copy the file /etc/localtime (symlink to /usr/share/zoneinfo/xyz) and the directory /usr/share/zoneinfo to your chroot.

如果您有 chroot 环境,则需要将文件 /etc/localtime(符号链接到 /usr/share/zoneinfo/xyz)和目录 /usr/share/zoneinfo 复制到您的 chroot。

Possible Error-Messages:

可能的错误信息:

Fatal error: phpinfo(): Timezone database is corrupt - this should *never* happen!  
Warning: phpinfo(): Invalid date.timezone value ‘UTC', we selected the timezone ‘UTC' for now.

See https://serverfault.com/questions/413293/php-fpm-chroot-jail-corrupts-timezone-db

https://serverfault.com/questions/413293/php-fpm-chroot-jail-corrupts-timezone-db