Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2.7: default timezone not set, causing fatal error

I'm trying to set up a symfony 2.7 based app on a shared server and have no permission to change the php.ini.

Executing: php app/console doctrine:schema:drop --force

Outputs this warnings/errors:

PHP Warning:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message '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.'
0 [internal function]: Symfony\Component\Debug\ErrorHandler-    >handleError(2, 'date_default_ti...', '/path...', 272, Array)
1 /domain.com/app/vendor/monolog/monolog/src/Monolog/Logger.php(272): date_default_timezone_get()
2 /domain.com/app/vendor/monolog/monolog/src/Monolog/Logger.php(481): Monolog\Logger->addRecord(100, 'Notified event ...', Array)
3 /domain.com/app/vendor/symfony/ in /domain.com/app/vendor/monolog/monolog/src/Monolog/Logger.php on line 272
PHP Fatal error:  date_default_timezone_get(): Timezone database is corrupt - this should *never* happen! in /domain.com/app/vendor/monolog/monolog/src/Monolog/Logger.php on line 272
PHP 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 /domain.com/app/vendor/monolog/monolog/src/Monolog/Logger.php on line 272
PHP Fatal error:  date_default_timezone_get(): Timezone database is corrupt - this should *never* happen! in /domain.com

I tried this:

class AppKernel extends Kernel
{
    public function init()
    {
        date_default_timezone_set( 'Europe/Berlin' );
        parent::init();
    }
}

Outputs the next error:

PHP Fatal error:  Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException' with message 'Notice: date_default_timezone_set(): Timezone ID 'Europe/Berlin' is invalid' in /domain.com/app/app/AppKernel.php:42

According to php.net Europe/Berlin is a valid identifier.

According to this answer timezonedb.so should be installed (it's not).

According to Symfony 1.4 docs it's possible to set default_timezone in the settings.yml. I cannot find a similar config for >= 2.0. Edit: "Unlike Symfony 1.4, there isn’t a default_timezone config parameter to set the default timezone in Symfony2. [...]" (source)

Any ideas, how to solve the problem?

Thanks in advance!

like image 835
Mr. B. Avatar asked Dec 05 '22 02:12

Mr. B.


2 Answers

it is recommended that you set the timezone in php.ini but if you are in shared environment or if you don't have access to it for some reason you can add this in app/AppKernel.php

class AppKernel extends Kernel
{
        public function __construct($environment, $debug)
        {
            date_default_timezone_set( 'America/Detroit' );
            parent::__construct($environment, $debug);
        }
}
like image 153
wonde Avatar answered Dec 09 '22 03:12

wonde


At less for me @wonde answer didn't work i don't know why, this worked for me

ini_set('date.timezone', 'Europe/Berlin');

Adding this line in your files :

/web/app_dev.php

/web/app.php

and if that doesnt work add in your kernel too.

That fix the problem for me !

like image 29
Carlos Delgado Avatar answered Dec 09 '22 01:12

Carlos Delgado