在 php.ini 文件中为 PHP 设置时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32224547/
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 22:51:41  来源:igfitidea点击:

Setting the Timezone for PHP in the php.ini file

phpdatetime

提问by Manas Chaturvedi

I'm trying to change the default PHP timezone to Asia/Calcuttaby accessing the /etc/php5/cli/php.inifile and changing

我正在尝试Asia/Calcutta通过访问/etc/php5/cli/php.ini文件并更改来更改默认的 PHP 时区

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

to

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Calcutta"

However, when I try to view current timezone under phpinfo(), the timezone follows the Europe/Berlintimezone.

但是,当我尝试在 下查看当前时区时phpinfo(),时区跟随Europe/Berlin时区。

I have tried stopping and restarting the Apache server, but that didn't seem to have changed the settings.

我曾尝试停止并重新启动 Apache 服务器,但这似乎并没有改变设置。

Is there something I'm missing in the process?

在这个过程中有什么我遗漏的吗?

PS: I'm using PHP 5.6.8 currently under XAMPP.

PS:我目前在 XAMPP 下使用 PHP 5.6.8。

回答by Manas Chaturvedi

I tried all the other possible solutions posted, but none of them helped. This is what helped me save my timezone settings:

我尝试了所有其他可能的解决方案,但没有一个有帮助。这是帮助我保存时区设置的原因:

1) Go to your phpinfo()page and search for Loaded Configuration Fileand open the php.inifile mentioned under that section.

1) 转到您的phpinfo()页面并搜索Loaded Configuration File并打开该php.ini部分下提到的文件。

2) Change the default timezone settings by adding your new timezone by modifying this line: date.timezone=Asia/Kolkata.

2)通过修改此行中添加新的时区更改默认的时区设置:date.timezone=Asia/Kolkata

3) Save the php.inifile and stop the Apache server.

3)保存php.ini文件并停止Apache服务器。

4) Restart the Apache server. The timezone settings should now be modified.

4)重启Apache服务器。现在应该修改时区设置。

回答by miller

You are changing the wrong file. The file /etc/php5/cli/php.iniis used by cli - command line interface. Don't forget to reboot your web-server after you update the right one: sudo service apache2 restartor sudo service php5-fpm restart

您正在更改错误的文件。该文件/etc/php5/cli/php.ini由 cli - 命令行界面使用。更新正确的网络服务器后,不要忘记重新启动您的网络服务器:sudo service apache2 restartsudo service php5-fpm restart

File locations for different OS:

不同操作系统的文件位置:

OS                               ->  Location

windows(With Xampp Installation) -> /xampp/php/php.in 

Mac OSX                          -> /private/etc/php.ini

Linux                            -> /etc/php.ini

                                 -> /usr/bin/php5/bin/php.ini

                                 -> /etc/php/php.ini

                                 -> /etc/php5/apache2/php.ini

回答by Daniel Krom

You can change it in the code without touching the ini file, at the beginning of your code add:

您可以在代码中更改它而无需触及 ini 文件,在代码的开头添加:

date_default_timezone_set('Asia/Calcutta')

回答by Patrick

  1. If you 're serious about web developping get a VM going(Virtual Box + Vagrant).
  2. With XAMPP be careful. Sometimes there's more than 1 php.ini file and you might be editing the wrong one. To edit the real one, access it from the XAMPP control panel.
  3. You can use the a function in your code: date_default_timezone_set();
  1. 如果您对 Web 开发很认真,请使用 VM(Virtual Box + Vagrant)。
  2. 使用 XAMPP 时要小心。有时有 1 个以上的 php.ini 文件,您可能正在编辑错误的文件。要编辑真实的,请从 XAMPP 控制面板访问它。
  3. 您可以在代码中使用 a 函数: date_default_timezone_set();

回答by Ganesh Patel

Check configuration in your system.

检查系统中的配置。

In my case, it was "Ubuntu"

就我而言,它是“ Ubuntu

sudo cat /etc/timezone Asia/Kolkata

回答by Shane Skidmore

I had the same problem and discovered that there are two date.timezone statements in php.ini in my version of XAMPP out of the box. The first statement one is commented out, but the second one is set to Europe/Berlin. So you if you edit your php.ini and search for timezone and logically uncomment the first statement with your time zone, the second statement overrides that back to Europe/Berlin.

我遇到了同样的问题,并发现在我的 XAMPP 版本中的 php.ini 中有两个 date.timezone 语句。第一个语句已被注释掉,但第二个语句设置为 Europe/Berlin。因此,如果您编辑 php.ini 并搜索时区并在逻辑上用您的时区取消注释第一条语句,则第二条语句会将其覆盖回欧洲/柏林。