PHP DateTime throws timezone warning even though date.timezone set
DateTime::createFromFormat(): 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
date.timezone = Asia/Jakarta
6 Answers 6
You’re just missing some quotes.
date.timezone = "Asia/Jakarta"
It could also be that you are not loading the correct php.ini file. See this post for more info: php5.3.3 date.timezone again php.ini directive not taken into account
date_default_timezone_set("Asia/Jakarta")
at the beginning of your script
Maybe this process running in php cli so, you need put this configuration inside your CLI php.ini in the following path: /etc/php5/cli/php.ini
By default the «date.timezone» was commented, so change this config: date.timezone = Asia/Jakarta
For those who are having the same problem in Symfony 3 add the following constructor into your app/AppKernel.php file and restart nginx.
public function __construct($environment, $debug) < date_default_timezone_set('Asia/Jakarta'); parent::__construct($environment, $debug); >
I also have specified the DateTime settings in my php.ini. In my case, the php.ini I edited was on
/Applications/MAMP/bin/php/php7.1.1/conf/php.ini
since I saw the path from info.php (in «Loaded Configuration File») that I generated on localhost.
Turns out it is not the one used. Try this instead:
And see what the output is. Mine was:
/usr/local/etc/php/5.6 Loaded Configuration File => /usr/local/etc/php/5.6/php.ini PHP Warning: Unknown: 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 Unknown on line 0
And then I just go to «/usr/local/etc/php/5.6» and edited the date.timezone line: removing comment (;) and added «Europe/Berlin» (or in your case «Asia/Jakarta».)
Try «php -i | grep php.ini» once again and the php warning message should be gone by now.
php -i | grep php.ini Configuration File (php.ini) Path => /usr/local/etc/php/5.6 Loaded Configuration File => /usr/local/etc/php/5.6/php.ini
PHP Question — Safe mode error when setting timezone
I’ve located the offending file and section of code (below). How would I fix this code? What is PHP_TZ supposed to do? Why doesn’t PHP like it? What can I do instead?
//set the timezone if ($configdata["timezone"] != "") < putenv("PHP_TZ=" . stripslashes($configdata["timezone"])); putenv("TZ=" . stripslashes($configdata["timezone"])); //for >= PHP 5.1 if(function_exists("date_default_timezone_set"))
I’m on PHP 5.2.10. I tried both ‘Europe/Zurich’, and ‘UTC’ for the values for $configdata[«timezone»] and got the same error for both.
2 Answers 2
PHP_TZ stands for PHP Timezone.
Since the version 5.1 you need to set it through the date_default_timezone_set function or from your PHP configuration. From the documentation :
Note:
Since PHP 5.1.0 (when the date/time functions were rewritten), every call to a date/time function will generate a E_NOTICE if the timezone isn’t valid, and/or a E_WARNING message if using the system settings or the TZ environment variable.
The easiest fix you could do is the following.
//set the timezone if ($configdata["timezone"] != "") < //for >= PHP 5.1 if(function_exists("date_default_timezone_set")) < date_default_timezone_set($configdata["timezone"]); // for PHP < 5.1 >else < putenv("PHP_TZ=" . stripslashes($configdata["timezone"])); putenv("TZ=" . stripslashes($configdata["timezone"])); >>
By default you SHOULD be able to set it. See bold section below. It seems your hosting provider disabled it via safe_mode_protected_env_vars .
Adds setting to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.
Setting certain environment variables may be a potential security breach. The safe_mode_allowed_env_vars directive contains a comma-delimited list of prefixes. In Safe Mode, the user may only alter environment variables whose names begin with the prefixes supplied by this directive. By default, users will only be able to set environment variables that begin with PHP_ (e.g. PHP_FOO=BAR). Note: if this directive is empty, PHP will let the user modify ANY environment variable!
The safe_mode_protected_env_vars directive contains a comma-delimited list of environment variables, that the end user won’t be able to change using putenv(). These variables will be protected even if safe_mode_allowed_env_vars is set to allow to change them.
HoLyVieR’s solution sounds like a good idea.
Can’t set default timezone in PHP
phpinfo() also shows that correct ini file is being used. I’ve restarted — but problem persisted. Then I did:
sudo service nginx stop sudo service apache2 start
Make sure you updated the correct php.ini. ofttimes there’s more than one on a server. You can find the correct one using phpinfo()
I’m testing from cli also. file only contains this: echo PHP_EOL . ‘timezone: ‘ . date_default_timezone_get();
@Stann — This question is kinda old and you may have figured it out alright. But if you haven’t, you needed to restart the php-fpm pool. I had to run /etc/init.d/php-fpm-5.5 restart to get this to work on a CentOS system. Restarting nginx alone wouldn’t help.
5 Answers 5
You probably edited the wrong php.ini . See php_info() (or php -i for the cli interpreter) which one is used. For example on ubuntu (and probably other linux distributions) its /etc/php5/cli/php.ini for the cli-interpreter, /etc/php5/apache/php.ini/ for the one used by Apaches mod_php and /etc/php5/cgi/php.ini used by php5-cgi (which is used by nginx ).
Yep. I thought of that. So I actually change date.timezone in all php.ini config files: cli, apache2, cgi, fpm. It does work correctly with cli and apache2. it doesn’t in fpm.
List of supported timezone values: http://php.net/timezones
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:
Get the process ids for php-fpm
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
service php5-fpm start or /etc/init.d/php5-fpm start
default timezone error in php
I got the following error/warning while tring to install Kohana/SilverStripe.
What does it mean and What do I do for it?
Warning: date_default_timezone_get(): 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 'Asia/Calcutta' for '5.5/no DST' instead in C:\Server\apache\htdocs\kohana\system\core\Kohana.php on line 136
I hate those kinds of cryptic error messages where it leaves you with no idea of how it was triggered, why it’s an error in the first place, and how to fix it. wait
I really don’t see this as a cryptic message, it says very clearly that the timezone was not set properly and you should use data_default_timezone to do so.
problem is that I looked up in the php.ini and saw that default time zone could be set. I thought giving explicit definition there would resolve matters that it would not generate the warning. But it turned out elsewise.
3 Answers 3
This is not an error, but a warning, so it does not block your app from working.
Explicitly set the right timezone using date_default_timezone_set() in C:\Server\apache\htdocs\kohana\system\core\Kohana.php on line 136
Edit
As the warning message itself states you actually have a more clean choice then editing a third party software file. I.e. configuring PHP as it should be.
- use a php_value directive in your web server config to set it in your vhost configuration or .htaccess : php_value date.timezone America/New_York
Suggesting to modify a framework or libraries core-files? That’s not a good suggestion as it is the wrong place and will break updates. Instead fix the servers or applications PHP configuration as outlined in the other answer, which is the concrete place to modify such.
@hakre I agree, it is a fix-the-issue answer .. In fact the answer is already given by the error message, I simply reported the simpler to implement for a not-so-skiled user (otherwise he already had fixed it using the warn message suggestion) .. I’ll edit my answer to be more complete. Thank you
The OP’s comment to his own question above implies that the default timezone cannot be set in php.ini, which is not true — in fact, in many cases setting it there is preferred. And since this SO answer is one of the first hits in Google for «php default timezone error», I’ll save others some lost sanity.
A very common cause of this error is not so much to misspell the zone city or country (e.g. «New York», «Los Angeles») in the php.ini file being used, but instead, to include embedded spaces rather than underscores in the identifier, and thus «misspell» the full constant.
This will also do what you want, without having to set it on every page at runtime (verified on PHP 5.4 and 5.3, in Windows, Linux and OSX):
[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = America/New_York ; ^^^^^^^^^^^^^^^^ * Must_Use_Underscores *
Also, if you’ve checked and re-checked the spelling and are certain it’s right, make sure you’re editing the actual php.ini file that’s being used:
Web context (create a test file test.php, and then confirm the date.timezone there):
The precedence order for retrieving the default date timezone configuration is spelled out in detail here.