PHP DateTime throws timezone warning even though date.timezone set - php

I am constantly getting the following error :
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
Although I've specified the DateTime settings in my php.ini like this:
date.timezone = Asia/Jakarta
Any idea?

You're just missing some quotes.
date.timezone = "Asia/Jakarta"
Best of luck!
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

You can use
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:
php -i | grep php.ini
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

Still not working with the above solutions, try to add the following code in .htaccess of your root directory and check.
php_value date.timezone America/Denver

Related

PHP: How to set timezone correctly? [duplicate]

I'm trying to install Symfony on XAMPP and I keep getting numerous errors.
[Symfony\Component\Debug\Exception\ContextErrorException]
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 the timezone 'UTC' for now, but please set
date.timezone to select your timezone. in
/Applications/XAMPP/xamppfiles/htdocs/vendor/monolog/monolog/src/Monolog/Logger.php line 233
and then
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-
install-cmd event terminated with an exception
and finally
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command.
I've tried changing the date.timezone in my php.ini file and using the date_default_timezone_set() function in the command line before trying to install it and nothing seems to work.
I've been staring at it for a while so any help is appreciated
If you can't set it correctly in your php.ini for some reason then you can set it as the first thing in your AppKernel, like so..
class AppKernel extends Kernel
{
public function __construct($environment, $debug)
{
date_default_timezone_set('Europe/London');
parent::__construct($environment, $debug);
}
public function registerBundles()
{
$bundles = array(
....
);
}
....
}
Found a similar way to fix this issue while other didn't.
First check where the CLI php.ini is located:
php -i | grep "php.ini"
In my case I ended up with : Configuration File (php.ini) Path => /etc
Then cd .. all the way back and cd into /etc, do ls in my case php.ini didn't show up, only a php.ini.default
Now, copy the php.ini.default file named as php.ini:
sudo cp php.ini.default php.ini
In order to edit, change the permissions of the file:
sudo chmod ug+w php.ini
sudo chgrp staff php.ini
Open directory and edit the php.ini file:
open .
Tip: If you are not able to edit the php.ini due to some permissions issue then copy 'php.ini.default' and paste it on your desktop and rename it to 'php.ini' then open it and edit it following step 7. Then move (copy+paste) it in /etc folder. Issue will be resolved.
Search for [Date] and make sure the following line is in the correct format:
date.timezone = "Europe/Amsterdam"
It is very very common for WAMP/XAMPP and other stacks to have use of multiple php.ini files - one for cli and one for web.
Since you're expiriencing the error from CLI try the following:
php -i | grep "php.ini"
this will output location of php.ini used for cli. Edit the date.timezone there.
As for the web mode, create a file foo.php within your "web" directory of symfony containing only"
<?php phpinfo(); ?>
And, again, find the location of php.ini used and edit the date.timezone.
Edit your php.ini (not necessarily at /etc/php.ini - use php -i | grep ini to find your version) and add your timezone.
E.g. for America / New York it would be:
date.timezone = "America/New_York"
See http://php.net/manual/en/timezones.america.php
It's been mentioned but this is as simple as:
sudo cp /etc/php.ini.default /etc/php.ini
You can't set the timezone if it can't find the php.ini file!!!!

Symfony2 Set the "date.timezone" setting in php.ini Errror

Just started my first symfony2 project, and after installation on running http://127.0.0.1/inventory/web/config.php i got below message.
This script will guide you through the basic configuration of your project. You can also do the same by editing the ‘app/config/parameters.yml’ file directly.
**MAJOR PROBLEMS**
Major problems have been detected and must be fixed before continuing:
Set the "date.timezone" setting in php.ini* (like Europe/Paris).
* Changes to the php.ini file must be done in "/etc/php5/apache2/php.ini".
I have set date.timezone = "Asia/Karachi" in following files
/etc/php5/cli/php.ini
/etc/php5/apache2/php.ini
and this too in AppKernel.php
public function init()
{
// get rid of Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone
date_default_timezone_set( 'Asia/Karachi' );
parent::init();
}
but still the same error.
however, when i run this php -i | grep "timezone"command on console i got this,
Default timezone => Asia/Karachi
date.timezone => Asia/Karachi => Asia/Karachi
More on running phpinfo() function on test.php file i got this,

Symfony 2 php.ini date.timezone

I have a problem with the setup of Symfony 2 localy on xampp.
The installation was no problem but when I execute the check.php it says:
Your system is not ready to run Symfony2 projects
* date.timezone setting must be set
> Set the "date.timezone" setting in php.ini* (like Europe/Paris).
But the date.timezone in the php.ini is already set correctly.
you can add this to your AppKernel.php
public function init()
{
// get rid of Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone
date_default_timezone_set( 'Europe/Paris' );
parent::init();
}
for some reason you have to change the date.timezone in
php.ini
in the
cli folder
got the hint when I tried to start a new project in Symfony3.
even if phpinfo() shows that the
php.ini file is in apache2 folder
If you are facing following problem :
Your system is not ready to run Symfony2 projects
* date.timezone setting must be set
> Set the "date.timezone" setting in php.ini* (like Europe/Paris)
Simply replace the |localizeddate('long','long') with |date('r') in app/Resources/views/base.html.twig file.
What worked for me is to add the -k option when restarting Apache

Symfony2 Date.TimeZone() Issue

I have tried everything under the sun to enable the ability to run this command after installing Symfony2.
php app/console cache:clear --no-warmup OR php app/console cache:clear
I get this error every time:
[ErrorException] 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /Applications/XAMPP/xamppfiles/htdocs/rt/vendor/jms/serializer-bundle/JMS/SerializerBundle/DependencyInjection Configuration.php line 66
On line 66:
->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
I've tried this Stack Post:
Warning: date_default_timezone_get():
I've tried to edit ALL php.ini files, phpinfo() for XAMPP shows:
date/time support enabled
"Olson" Timezone Database Version 2013.4
Timezone Database internal
Default timezone UTC
$ php --ini shows
Configuration File (php.ini) Path: /etc (I edited this one as well)
php.ini shows
date.timezone = UTC
I cannot run php composer.phar install without the ability to get past this stupid date_default_timezone_set() error.
Please help, its been 2 haggling days trying to get this fixed and I am at my wits end.
Thanks!
I've solved this issue adding this code to the app/AppKernel.php file:
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel {
public function init() {
// Change timezone
date_default_timezone_set('America/Caracas');
parent::init();
}
// ......
}
A good option is also to add the following into the virtualhost file (if applicable)
php_value date.timezone America/Caracas
Add the below in your virtualhost file as suggested above
php_value date.timezone America/Caracas
You can also have the same in the .htaccess file in the web folder of your symfony2 instead.
Check the php.ini under both
/etc/php5/apache2/ ( for apache2 )
and
/etc/php5/cli/ ( for command line )
You can add a default timezone to your php.ini
http://www.inmotionhosting.com/support/website/php/setting-the-timezone-for-php-in-the-phpini-file
Don't forget to restart the server

Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings

I have a Symfony2 project. I updated my php to 5.5.7 today and since then, I am getting the
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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. in...
I setup the default timezone in my php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Paris";
To be sure that this is the good php.ini, I was checking with
phpinfo();
And the path I ma getting there is the one I am modifying:
/usr/local/php5.5.7/lib
But in there, I see the
Default timezone UTC
Which is strange.
any idea? Thank you.
Maybe you are trying to set it in Apache's php.ini, but your CLI (Command Line Interface) php.ini is not good.
Find your php.ini file with the following command:
php -i | grep php.ini
And then search for date.timezone and set it to "Europe/Amsterdam". all valid timezone will be found here http://php.net/manual/en/timezones.php
Another way (if the other does not work), search for the file AppKernel.php, which should be under the folder app of your Symfony project directory. Overwrite the __construct function below in the class AppKernel:
<?php
class AppKernel extends Kernel
{
// Other methods and variables
// Append this init function below
public function __construct($environment, $debug)
{
date_default_timezone_set( 'Europe/Paris' );
parent::__construct($environment, $debug);
}
}
Found a similar way to fix this issue (at least it did for me).
First check where the CLI php.ini is located:
php -i | grep "php.ini"
In my case I ended up with : Configuration File (php.ini) Path => /etc
Then cd .. all the way back and cd into /etc, do ls in my case php.ini didn't show up, only a php.ini.default
Now, copy the php.ini.default file named as php.ini:
sudo cp php.ini.default php.ini
In order to edit, change the permissions of the file:
sudo chmod ug+w php.ini
sudo chgrp staff php.ini
Open directory and edit the php.ini file:
open .
Tip: If you are not able to edit the php.ini due to some permissions issue then copy 'php.ini.default' and paste it on your desktop and rename it to 'php.ini' then open it and edit it following step 7. Then move (copy+paste) it in /etc folder. Issue will be resolved.
Search for [Date] and make sure the following line is in the correct format:
date.timezone = "Europe/Amsterdam"
I hope this could help you out.
The current accepted answer by crack is deprecated in Symfony 2.3 and will be removed by 3.0. It should be moved to the constructor:
public function __construct($environment, $debug) {
date_default_timezone_set('Europe/Warsaw');
parent::__construct($environment, $debug);
}
Since PHP 5.5, there is a separate php.ini file for CLI interface.
If You use symfony console from command line, then this specific php.ini is used.
In Ubuntu 13.10 check file:
/etc/php5/cli/php.ini
The problem appear when we are using PHP 5.1 on Redhat or Cent OS
PHP 5.1 on RHEL/CentOS doesn't support the timezone functions
Following up on sas's answer, PHP 5.4, Symfony 2.8, I had to use
ini_set('date.timezone','<whatever timezone string>');
instead of date_default_timezone_set. I also added a call to ini_set to the top of a custom web/config.php to get that check to succeed.
add this code to Your AppKernel Class:
public function init()
{
date_default_timezone_set('Asia/Tehran');
parent::init();
}

Categories