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

前端 未结 24 1947
予麋鹿
予麋鹿 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:33

    If you are using CodeIgniter and can't change php.ini, I added the following to the beginning of index.php:

    date_default_timezone_set('GMT');
    
    0 讨论(0)
  • 2020-11-22 11:33

    In my particular case, I have PHP configured to use PHP-FPM (FastCGI Process Manager). When executing phpinfo() from CLI I saw correct timezone that I had set in the php.ini, however it was still incorrect in the browser and causing my code to fail. I simply needed to restart the php-fpm service on the server.

    service rh-php56-php-fpm restart
    

    You may also need to restart the httpd service if you edited the php.ini

    service httpd restart
    
    0 讨论(0)
  • 2020-11-22 11:36

    If these are not your options

    • Modifying php.ini.
    • Adding date_default_timezone call.

    Instead of date you could use gmdate.

    I have used gmdate( "Y" ) when I needed a year for a copyright snipplet.

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

    If you cannot modify your php.ini configuration, you could as well use the following snippet at the beginning of your code:

    date_default_timezone_set('Africa/Lagos');//or change to whatever timezone you want
    

    The list of timezones can be found at http://www.php.net/manual/en/timezones.php.

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

    There are two options for the resolve this

    First, change into the php.ini file and put default timezone

    date.timezone = "America/New_York"
    

    Once, you have set the timezone in php.ini restart the server

    Secondly, Change the run time assign time zone based on your need

    date_default_timezone_set('America/New_York');
    
    0 讨论(0)
  • 2020-11-22 11:43

    This answer above from CtrlX is the correct answer, but it may not work completely. I added this line to my php.ini file:

    date.timezone = "America/Los_Angeles"
    

    but it did not remove the PHP error for all my files because some of my PHP scripts are in subfolders. So I had to edit .htaccess file to setup php.ini to be used recursively (in subfolders):

    suphp_configpath /home/account_name/public_html
    

    where account_name is your cpanel account name and public_html is the folder your php.ini file is in.

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