“date(): It is not safe to rely on the system's timezone settings…”

前端 未结 24 1948
予麋鹿
予麋鹿 2020-11-22 11:06

I got this error when I requested to update the PHP version from 5.2.17 to PHP 5.3.21 on the server.

相关标签:
24条回答
  • 2020-11-22 11:48

    I am hosting my EC2 and S3 bucket in us-west-2 (Oregon) region. When I was calling $s3client->listBuckets() to list existing buckets in my php, I was getting exception - "Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings...". I made below changes to make it work. Sharing these details in case someone is facing similar issue and any of above answers have not helped.

    1. Based on documentation @ AWS Configuring Network Time Protocol (NTP), I confirmed ntpd service status is fine by running ntpstat command. If you get error, this link helps you to know what is going wrong.
    2. Opened /etc/php.ini file (some may have it in different path based on version of php installed) and found that date.timezone was not set to any value and also it was commented by default. I un-commented by removing ';' before this line and set its value to "UTC" and saved the file.
    3. Restarted http and ntp daemons using sudo service httpd restart and sudo service ntpd restart commands.

    After this I am able to list buckets successfully without any exception. Hope this helps.

    0 讨论(0)
  • 2020-11-22 11:49

    A quick solution whilst your rectify the incompatibilities, is to disable error reporting in your index.php file:

    Insert the line below into your index.php below define( ‘_JEXEC’, 1 );

    error_reporting( E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR |
    E_COMPILE_WARNING );
    
    0 讨论(0)
  • 2020-11-22 11:51

    For docker user: Create a local timezone.ini file in your project, set up the config in docker-compose.yml,

        volumes:
            - "./docker/config/timezone.ini:/usr/local/etc/php/conf.d/timezone.ini"
    

    timezone.ini

        date.timezone=Australia/Sydney
    
    0 讨论(0)
  • 2020-11-22 11:52

    You probably need to put the timezone in a configuration line in your php.ini file. You should have a block like this in your php.ini file:

    [Date]
    ; Defines the default timezone used by the date functions
    ; http://php.net/date.timezone
    date.timezone = America/New_York
    

    If not, add it (replacing the timezone by yours). After configuring, make sure to restart httpd (service httpd restart).

    Here is the list of supported timezones.

    0 讨论(0)
  • 2020-11-22 11:52

    @Justis pointed me to the right direction, but his code did not work for me. This did:

    // set the default timezone if not set at php.ini
    if (!date_default_timezone_get('date.timezone')) {
        // insert here the default timezone
        date_default_timezone_set('America/New_York');
    }
    

    Documentation: http://www.php.net/manual/en/function.date-default-timezone-get.php

    This solution is not only for those who does not have full system access. It is necessary for any script when you provide it to anyone else but you. You never know on what server the script will run when you distribute it to someone else.

    0 讨论(0)
  • 2020-11-22 11:52

    I had this error running php-fpm in a chroot jail. I tried creating etc/php.ini and /usr/share/zoneinfo in the chroot directory, but it just didn't work. I even tried strace-ing the php-fpm daemons to see what file they were missing - nothing jumped out.

    So in case Google brings you here because you get this error when using php-fpm configured for chroot, you can probably fix it by adding this line to /etc/php-fpm.d/www.conf in the ENV section:

    env[TZ] = America/New_York
    

    A php-fpm restart is normally required for it to take effect. Hope this helps somebody out there.

    0 讨论(0)
提交回复
热议问题