date(): It is not safe to rely on the system's timezone settings in CodeIgniter

本文介绍了解决PHP中使用date()函数遇到的时区配置错误问题,提供了两种解决方案:一是修改php.ini文件设置默认时区;二是通过代码设置默认时区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://androidren.com/index.php?qa=277&qa_1=date-not-safe-rely-the-systems-timezone-settings-codeigniter

PHP代码中调用date()时,遇到如题的错误。

原因是:服务器没有配置时区。

解决方法有两种:

1.修改php.ini

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.

2.修改调用地方的代码

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.

另外,还有奇葩的解决方案:

Add the following in your index.php file. I first came across this when I moved my application from my XAMPP server to Apache 2.2 and PHP 5.4...

I would advise you do it in your index.php file instead of the php.ini file.

if( ! ini_get('date.timezone') )

{

    date_default_timezone_set('GMT');

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值