Fix PHP Error – It is not safe to rely on the system’s timezone settings

Updated on October 24, 2017

Do you see this error “It is not safe to rely on the system’s timezone settings” (PHP timezone setting error)  while executing a PHP script? I recently got this error after updating PHP on CentOS machine. Below is the complete error when mysqli_connect failed connecting to the database.

Warning: mysqli_connect(): 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 /var/www/html/mysqli_connect.php on line 7

Here’s the solution for the above error.

How to Fix PHP timezone setting error

Starting from PHP version 5.3, you need to set timezone in the PHP script using date_default_timezone_set() function before calling date() function or set it globally in php.ini file.

To set the desired timezone in php.ini, follow the below steps.

Step 1: Edit php.ini

# vim /etc/php.ini
Note:

The location of PHP.ini might be different on your machine.

If you don’t have a permission to edit php.ini, then you may create one in  document root as explained in this tutorial.

Step 2: Lookout for [Date] section

Step 3: Set your desired timezone in date.timezone attribute as shown below:

date.timezone = 'Asia/Kolkata'
Note:

You might want to set a different timezone. Here’s a list of timezone supported in PHP.

Step 4: Save the file and quit

Step 5: Reload web server. If you are using Apache web server, then below is the command.

# /etc/init.d/httpd reload

(or)

You can set timezone using the below function:

date_default_timezone_set('Asia/Kolkata');

The above function will return FALSE for an invalid timezone. The date_default_timezone_set() has to be used before calling date() function in your application.

Learn more about date_default_timezone_set function PHP official documentation.

Was this article helpful?

Related Articles

Leave a Comment