Tuesday, October 1, 2013

How to fix "It is not safe to rely on the system's timezone settings"

If you see one of following warnings when you run a PHP script:


  • PHP Warning:  mktime(): 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 'IST/5.0/no DST' instead in /opt/websites/www/sample.php on line 10
  • 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 'Asia/Calcutta' for 'IST/5.0/no DST' instead in  /opt/websites/www/sample.php on line 11
PHP Warning:  mktime(): It is not safe to rely on the system's timezone settings.
PHP Warning:  date(): It is not safe to rely on the system's timezone settings.

You can try following solutions:

Solution 01

  1. Find your timezone settings

    (For Ubuntu)
    cat /etc/timezone 

    (For Fedora/CentOS/RedHat)
    cat /etc/sysconfig/clock 

     

    Figure:  Ubuntu 12.04 Terminal

    or simply select your timezone from http://php.net/manual/en/timezones.php
  2. Find your php.ini file to edit

    (For Ubuntu)
    cat /etc/php5/apache2/php.ini

    (For Fedora/CentOS/RedHat)
    cat /etc/php.ini

    or You can locate all php.ini files using
    locate php.ini

  3. Edit php.ini

    Now insert following date-time settings into php.ini (replace "Asia/Colombo" using your timezone settings)
    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = Asia/Colombo
  4. Restart Apache

    (For Ubuntu)
    /etc/init.d/apache2 restart
    or
    service apache2 restart
    (For Fedora/CentOS/RedHat)
    /etc/init.d/httpd restart
    or
    service httpd restart

Solution 02

  1. Edit your php script

    if you are unable to access your php.ini, can set the default timezone at the beginning of your PHP scripts.
    <?php
    date_default_timezone_set("Asia/Colombo");

4 comments:

  1. It is not recommended to use scripts in the manner `/etc/init.d/SERVICE ACTION`. Instead use `service SERVICE ACTION`.

    ReplyDelete
    Replies
    1. True, I also recommend to use "service SERVICE ACTION"

      Delete