78

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.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • 2
    can you confirm me the full path of the php.ini that you have changed – sas Dec 23 '13 at 12:02
  • 2
    if you get this warning in symfony console then you also need to change at cli/php.ini – sas Dec 23 '13 at 12:05

7 Answers7

150

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);
    }

}
sas
  • 2,563
  • 1
  • 20
  • 28
  • Thank you, but already checked: Loaded Configuration File => /usr/local/php5.5.7/lib/php.ini – Milos Cuculovic Dec 23 '13 at 12:19
  • there should be another configuration for cli, did you check that also? – sas Dec 23 '13 at 12:29
  • Hm, and how to find this second one? – Milos Cuculovic Dec 23 '13 at 12:34
  • may I know how you have installed php5.5.7? because normally it should have been placed at etc/php5 anyway you can fix this by set default timezone at karnel class constructor. – sas Dec 23 '13 at 12:48
  • I installed it from source with ./configure --prefix=/usr/local/php5.5.7 – Milos Cuculovic Dec 23 '13 at 13:02
  • Thank you for your answer. I think I know where the problem is. When restarting the php-fpm, I am restarting the old one, do you know how to restart the new php5.5.7 I installed ? – Milos Cuculovic Dec 23 '13 at 13:15
  • 1
    restarting your apache2 will suppose to restart php as well – sas Dec 23 '13 at 13:32
  • 1
    i had the same problem and your another solution works with me – ahmed hamdy Mar 21 '14 at 19:43
  • perfect, saved me lot of time! –  Oct 02 '15 at 17:04
  • This worked great! It may be valuable to add a link to valid timezone values: http://php.net/manual/en/timezones.php – kevnk May 13 '16 at 13:54
  • Thanks, I have added – sas May 18 '16 at 10:04
  • Hi! in my case i use XAMPP on MacOS, then i do `php -i` and output this: `Configuration File (php.ini) Path => /etc` `Loaded Configuration File => (none)` And that's was the problem, in /etc there's no php.ini file, i've created with `sudo nano /etc/php.ini` and add this entry [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone=Europe/Madrid and start working, i've tried the @sas solution and work like a charm too. – Ricardo Martínez Jul 08 '17 at 00:59
  • I exactly don't where is the bin/php of xampp on mac. please try to find it, should be in the directory of xampp. e.g. run DIR_XAMPP/bin/php -i – sas Jul 11 '17 at 13:47
  • `php --ini` is a nice shortcut – vp_arth Oct 10 '17 at 10:47
66

Found a similar way to fix this issue (at least it did for me).

  1. First check where the CLI php.ini is located:

    php -i | grep "php.ini"

  2. In my case I ended up with : Configuration File (php.ini) Path => /etc

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

  4. Now, copy the php.ini.default file named as php.ini:

    sudo cp php.ini.default php.ini

  5. In order to edit, change the permissions of the file:

    sudo chmod ug+w php.ini

    sudo chgrp staff php.ini

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

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

Peter
  • 1,049
  • 8
  • 12
25

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);
}
Krzysztof Bociurko
  • 4,575
  • 2
  • 26
  • 44
2

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

Valentas
  • 2,135
  • 1
  • 17
  • 21
1

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

Community
  • 1
  • 1
bilelovitch
  • 1,975
  • 1
  • 16
  • 24
1

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.

cxw
  • 16,685
  • 2
  • 45
  • 81
-1

add this code to Your AppKernel Class:

public function init()
{
    date_default_timezone_set('Asia/Tehran');
    parent::init();
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Sifb71
  • 41
  • 10
  • `init` is deprecated since version 2.3, to be removed in 3.0. Move your logic in the constructor instead like @Krzysztof Bociurko or the updated accepted answer. – user276648 Oct 18 '16 at 06:22