简体   繁体   中英

Cannot Set date.timezone In php.ini file

I'm using php5.5 and getting this error whenever I used the date function in PHP:

 Warning: phpinfo(): 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/info.php on line 1

the loaded configuration file is here:

/etc/php5/apache2/php.ini 

so I changed the date.timezone setting into this:

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

; http://php.net/date.default-latitude
;date.default_latitude = 31.7667

; http://php.net/date.default-longitude
;date.default_longitude = 35.2333

; http://php.net/date.sunrise-zenith
;date.sunrise_zenith = 90.583333

; http://php.net/date.sunset-zenith
;date.sunset_zenith = 90.583333

Then I restart the server:

sudo /etc/init.d/apache2 restart

but still getting this error, I tried to check the .ini file in the Additional ini file location but none of it is overriding the date.timezone setting

I've checked the php.ini file permission, but still not working please guide me to solve this problem, thanks..

Try this:

date.timezone = "Asia/Jakarta"

Edit:

Let's locate the correct php.ini. Fire this within apache / nginx / whatever web server deamon your using (because the command line version may be different by the one used by it).

<?php
phpinfo();
?>

What's the .ini in the output? Still the same?

Edit2:

After the php.ini edit, just restart the webserver (apache or nginx) ...

finally solved my problem, this is my Loaded Configuration File:

/etc/php5/apache2/php.ini 

modified the date.timezone here but it's not working.

So, I check the "Scan this dir for additional .ini files " values in phpinfo() which point to:

/etc/php5/apache2/conf.d 

then I search date.timezone from all files in that folder but found none.

this is my Additional .ini file parsed value in phpinfo():

/etc/php5/apache2/conf.d/05-opcache.ini, /etc/php5/apache2/conf.d/10-pdo.ini, /etc/php5/apache2/conf.d/20-json.ini, /etc/php5/apache2/conf.d/20-mysql.ini, /etc/php5/apache2/conf.d/20-mysqli.ini, /etc/php5/apache2/conf.d/20-pdo_mysql.ini, /etc/php5/apache2/conf.d/20-xdebug.ini, /etc/php5/apache2/conf.d/30-mcrypt.ini 

I modified /etc/php5/apache2/conf.d/20-xdebug.ini, and appended this line:

date.timezone = Asia/Jakarta

very weird but this solved my problem !!!

You should check out by locate php.in, if there somewhere has another one.

for me. there should have CLI for command line interface:

/etc/php5/cli/php.ini
/etc/php5/fpm/php.ini

change the /etc/php5/cli/php.ini

It works for me now.

Another solution for me after compiling PHP from source was to check php.ini was actually loading a config file. From command line

php --ini

returns

Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Find it.

find -name "php.ini" #it was in /usr/local/php/php.ini
cp /usr/local/php/php.ini /usr/local/lib/php.ini

Restart php (in my case, /etc/init.d/php-fpm stop , /etc/init.d/php-fpm start ) and it works fine. For some reason it got copied wrong.

  • Distro: Centos 7
  • Web Server: Nginx
  • Type: EC2 Instance

I've faced the same issue recently with PHP 5.6.4.

  • PHP 5.6.4 behaves strange
  • PHP 5.6.11 behaves normally as expected

When I put the date.timezone configuration option under [PHP] section in the configuration file it works fine for PHP 5.6.4. For example I've added the option at the very beginning of the php.ini file

[PHP]
date.timezone = "Europe/Stockholm"

But when I put the configuration option how this is shown in the example under [Date] section it does not work as expected, it looks like the option is ignored

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
; this does not work for me for PHP 5.6.4 but works fine for PHP 5.6.11
date.timezone = "Europe/Stockholm"

When I add option to the 20-xdebug.ini like it is suggestion in the accepted answer https://stackoverflow.com/a/20731466/2111430 the option is accepted because this file has only [PHP] section. Out of curiosity I tried other included ini files and it looks like I can use any one.

One more time, the issue happened for me only with PHP 5.6.4. When I changed the option on the server with PHP 5.6.11 it behaved exactly like I expected.

In my case I had 2 date.timezone= records in the php.ini file and the last one was overriding the first one. removing one of them solved my problem.

Also don't forget you need to restart the web server process.

It worked for me when I noticed that the date.timezone entry in php.ini was commented out. After removing the semicolon the timezone was set correctly.

If you set the date.timezone value in php.ini but it does not work (ie. local value and master value are different) then you might need to update something like this in your apache configuration file:

 <IfModule mod_php5.c> php_value date.timezone "Europe/Paris" </IfModule>

检查 php.ini 中是否存在冲突设置,我遇到了同样的问题,在我的情况下,在 date.timezone 之前定义的 xdebug 设置导致了这件事:有关更多详细信息,请参阅: PHP Xdebug 问题 - date.timezone & session.save_path undefined

some command line php application installer (like the symfony installer, or even when php scripts are run from cli), use the ini file inside the php-cli directory

/etc/php5/cli/php.ini


this solved my similar issue in dependency checking process while installing Symfony.

我在 Mac OSX El Captain 中修复了这个问题,创建了一个文件夹结构:/Library/Server/Web/Config/php,然后使用 date.timezone 指令在这个新的 php 文件夹中创建一个 php.ini。

Iam using PHP 7.3.6.

  1. open xampp/php/php.ini
  2. go to line 1967
  3. set line date.timezone=Asia/Jakarta
  4. Restart Apache

在此处输入图片说明

Hope answer your question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM