PHP: How to set timezone correctly? [duplicate] - php

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!!!!

Related

Timezone in symfony

I'm trying to run new symfony project by symfony new name_project 2.7.21. Unfortunately, I keep having the error for incorrect time zone. I changed it in my C:\xampp\php\php.ini file. Do I have to change it somewhere else? Many thanks for your help
try
add in app/AppKernel.php
public function __construct($environment, $debug)
{
date_default_timezone_set( 'Europe/Warsaw' );
parent::__construct($environment, $debug);
}
I had the same problem with CI system , and that was solution
If your Symfony version is "^4.*" you can set your global timeZone in project/public/index.php
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
date_default_timezone_set( 'Europe/Moscow' );
$request = Request::createFromGlobals();
PHP supported timezone list below
https://www.php.net/manual/en/timezones.europe.php
Ubuntu
Though I had date.timezone set in the /etc/php5/apache2/php.ini file.
I had to Set the date.timezone = "Asia/Calcutta" in the php.ini file which was in the cli directory.
/etc/php5/cli/php.ini
This solved it for me...
If editing the ini file doesn't work, make sure you are editing the correct one. Check which ini file php is loading by outputting the phpinfo results.
Otherwise, you can hard-code the timezone in app.php and app_dev.php
See: http://php.net/manual/en/function.date-default-timezone-set.php
Hey so I spent a lot of time chasing this down so here's what worked. To find the real php.ini that you need to change go to Terminal and run the following:
php -i | grep "php.ini"
You'll get a path like this: Configuration File (php.ini) Path => /etc
When you go to that directory you may find that you, in fact, have no php.ini file at all but instead have something named php.ini.default !!!
Copy that bad boy in Terminal like so: sudo cp php.ini.default php.ini
Then open up your new php.ini file and edit the date.timezone = "Europe/Paris"
IMPORTANT!!! RESTART YOUR LOCAL SERVER!
Then go back to Terminal and run Symfony's complaining-ass validator like so: php my_project_name/bin/symfony_requirements

Symfony date.timezone error on Yosemite

I've created a new Symfony project with
$ symfony new my_project
This is now in a folder called /Symfony/my_project
At this point I get this:
PHP Warning: date(): 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 phar:///usr/local/bin/symfony/src/Symfony/Installer/NewCommand.php on line 262
✕ Symfony 2.7.3 was successfully installed but your system doesn't meet its
technical requirements! Fix the following issues before executing
your Symfony application:
* date.timezone setting must be set
> Set the "date.timezone" setting in php.ini* (like Europe/Paris).
Than says
Then, you can:
* Change your current directory to /Users/[username]/Dropbox/private/dbx/Symfony/my_project
* Configure your application in app/config/parameters.yml file.
* Run your application:
1. Execute the php app/console server:run command.
2. Browse to the http://localhost:8000 URL.
At this point I don't know where is the php.ini file in Yosemite. I've looked around and found an answer on this but the php.ini used is not the one I edited.
Which and where is the php.ini file to edit?
Try to modify the real php.ini, you can find it running php -i | grep "php.ini" on Terminal.
Remember that sometimes you'll find only a file called php.ini.default so you have to copy the file php.ini.default in a new php.ini file using sudo cp php.ini.default php.ini
Once you create the new php.ini file you have to change the permission in order to be able to modify the content:
sudo chmod ug+w php.ini
sudo chgrp staff php.ini
Now you can open the file php.ini and modify the date.timezone line.
NOTE: Remember to remove the comment if the line is commented.
If this doesn't work try to add the init function to your AppKernel.php file (code below), this really helped me.
<?php
class AppKernel extends Kernel
{
// Other methods and variables
// Append this init function below
public function init()
{
date_default_timezone_set( 'Europe/Paris' );
parent::init();
}
}
Since this is Terminal-based run and not browser based you can easily find that out. From your Terminal, run this:
php -i | grep "php.ini"
This will output the location of php.ini used. Then edit the file via any text editor (graphic or otherwise) and set the approprite time zone.
Hope this helps.

Why do I get a PHP date.timezone Warning each time I open the Terminal on MAC OS X?

why do I get the following PHP warning each time I open the Terminal.app on OS X?
Last login: Mon Dec 22 11:53:25 on ttys000
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
machine:~ user$
I have noticed this only today, previously I didn't have this Warning displayed.
I have PHP date.timezone set inside the php.ini:
[Date]
; Defines the default timezone used by the date functions
; Will be changed by MAMP to system timezone
date.timezone = "Europe/Moscow"
This directive is inside the php.ini located at /Applications/MAMP/bin/php/php5.6.2/conf/php.ini, which is the one PHP configuration file my PHP uses from the CLI also:
machine:~ user$ which php
/Applications/MAMP/bin/php/php5.6.2/bin/php
machine:~ user$ php -i | grep "Configuration File"
Configuration File (php.ini) Path => /Applications/MAMP/bin/php/php5.6.2/conf
Loaded Configuration File => /Applications/MAMP/bin/php/php5.6.2/conf/php.ini
I have also checked the php.ini of the pre-installed PHP inside /private/etc/php.ini (aliased by /etc/php.ini), and I have set the date.timezone directive there also.
So why do I still get this nasty warning?
Thank you for the attention!
EDIT: this is the output of php --ini:
Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php5.6.2/conf
Loaded Configuration File: /Applications/MAMP/bin/php/php5.6.2/conf/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
It seems that the only loaded conf file is the php.ini of MAMP, but why do I keep obtaining that warning when I start the terminal?
I have found the problem. It was this line inside the .bash_profile in my home folder:
export PHP_API_VER=`php -i | grep 'PHP Extension =' | grep -oE '[0-9]+'`
I use this variable to know which PHP API extension I am currently using. As this variable export was after this one:
PATH="$MAMP_PHP_BIN_FOLDER:$PATH"
Which adds the PHP bin folder (where the php command is located) to the PATH env var, I experienced that error because when opening Terminal.app the wrong php command was used I had to put PHP_API_VER export after the PATH one.
Then I asked myself "Why do I obtain that warning if I have both MAMP PHP and OS X pre-installed PHP .ini files with the date.timezone directive set?", and the answer was a which php when .bash_profile did load, which revealed:
/usr/local/bin/php
What?! I had another version of PHP installed I forgot about (I installed it with HomeBrew). So I checked the php.ini of that php command, added the date.timezone directive and everything is great and works now.
Bye!

Warning: date_default_timezone_get() installing Symfony

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 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