Date error in CodeIgniter when it host

时间:2017-12-18 05:31:49

标签: php codeigniter date timezone

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 the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/nuvactec/public_html/files/system/core/Log.php on line 176

this is error coming when i running codegniter program in host but no problem when running in localhost

2 个答案:

答案 0 :(得分:0)

The time_reference setting in the config file is only used by the date helper, and the only part of CI that uses the date helper is the Calendar library. The comment in the config file implies this:

See the 'date helper' page of the user guide for information regarding date handling.

...but the sentence before that implies a broader use in the system, which isn't the case:

This preference tells the system whether to use your server's local time as the master 'now'
reference, or convert it to the configured one timezone.

(I guess the "one" should be removed from that sentence.)

Personally, I avoid using the date helper because it creates discrepancies between the times generated by the date helper and those in the logs (since log_message() doesn't use the date helper or the time_reference setting).

The best thing to do if you can't modify the php.ini is to set the value yourself in your index.php file.

Since you're seeing the notices in your logs, you could probably get away with something simpler, like:

if (ini_get('date.timezone') == '') {
    date_default_timezone_set('UTC');
}

Then you would just replace 'UTC' with whatever timezone you wanted. If you're really stuck on using the config value, you would need to call the function later, after the config file has been loaded, which means there's still a chance that something would attempt to use the default timezone before it is set.

A pre_system hook could set the timezone, but I'm not 100% sure that PHP's microtime() is independent of the default timezone setting; the Benchmark class uses microtime() to set the total_execution_time_start and loading_time:_base_classes_start benchmarks before the Hooks class is loaded.

答案 1 :(得分:0)

This raises because your hosting provider is out of your timezone. Ex if you purchase AWS it can set to US timezone.

To handle this

Path - application/config.php

defined('BASEPATH') OR exit('No direct script access allowed');

date_default_timezone_set('America/New_York'); # add this

Read - List of Supported Timezones - php.net


What happens in the wrong timezone?

Assume your local time is 2017-12-18 10:10:00 and server time is 2017-12-18 22-10-00. For example, if your write Insert statement, timezone will be captured on server time. (most of us use to write update_at with date('Y-m-d H:i:s) )