如何为 CodeIgniter 设置 date.timezone 以使用 php 5.3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4882790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:10:51  来源:igfitidea点击:

How to set date.timezone for CodeIgniter to work with php 5.3

codeigniterdatetimephp

提问by Snowfish

When date.timezone in php.ini is commented out, it gives me:

当 php.ini 中的 date.timezone 被注释掉时,它给了我:

A PHP Error was encountered

Severity: Warning

Message: main(): It is not safe to rely on the system's timezone settings. You are requiredto 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 'America/Los_Angeles' for '-8.0/no DST' instead

Filename: controllers/helloworld.php

Line Number: 2

遇到 PHP 错误

严重性:警告

消息:main():依赖系统的时区设置是不安全的。您需要使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了这些方法中的任何一种并且仍然收到此警告,则很可能是您拼错了时区标识符。我们为“-8.0/no DST”选择了“America/Los_Angeles”

文件名:controllers/helloworld.php

行号:2

When I have

当我有

date.timezone = "America/Los_Angeles"

It gives me this:

它给了我这个:

Server error The website encountered an error while retrieving http://localhost/ci/index.php/helloworld. It may be down for maintenance or configured incorrectly. Here are some suggestions: Reload this web page later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

服务器错误 网站在检索http://localhost/ci/index.php/helloworld时遇到错误 。它可能因维护而停机或配置不正确。以下是一些建议: 稍后重新加载此网页。HTTP 错误 500(内部服务器错误):服务器尝试完成请求时遇到意外情况。

I am using php 5.3, CodeIgniter 2.0.0, and Apache 2.2.

我使用的是 php 5.3、CodeIgniter 2.0.0 和 Apache 2.2。

Update 1: I tried loading a test.php without CodeIgniter, where the first 3 lines of test.php is

更新 1:我尝试在没有 CodeIgniter 的情况下加载 test.php,其中 test.php 的前 3 行是

date_default_timezone_set('America/Los_Angeles');
echo date("l j \of F Y h:i:s A");

And it works fine, different timezones also works fine too. So I suspect the problem is from CodeIgniter.

它工作正常,不同的时区也工作正常。所以我怀疑问题出在 CodeIgniter 上。

回答by Phil Sturgeon

If you Googled "CodeIgniter PHP 5.3" you would have found this article pretty quickly :)

如果您在 Google 上搜索“CodeIgniter PHP 5.3”,您会很快找到这篇文章:)

http://philsturgeon.co.uk/blog/2009/12/CodeIgniter-on-PHP-5.3

http://philsturgeon.co.uk/blog/2009/12/CodeIgniter-on-PHP-5.3

To fix this, you only need to edit the main index.php for your CodeIgniter application:

要解决此问题,您只需编辑 CodeIgniter 应用程序的主 index.php:

if( ! ini_get('date.timezone') )
{
   date_default_timezone_set('GMT');
} 

This modification is something you will probably need to make for any CodeIgniter application running on PHP 5.3 and can easily be modified to your local timezone. There is a full list of supported timezones in the PHP manual here.

对于在 PHP 5.3 上运行的任何 CodeIgniter 应用程序,您可能需要进行此修改,并且可以轻松地将其修改为您的本地时区。在此处的 PHP 手册中有支持的时区的完整列表。

回答by Mavelo

Yes, if you cannot directly edit the php.ini file, placing...

是的,如果你不能直接编辑 php.ini 文件,放置...

ini_set('date.timezone', 'America/New_York');

...as the first line in CI's index.php works fine.

...因为 CI 的 index.php 中的第一行工作正常。

Reference: PHP's Available Timezones

参考:PHP 的可用时区

回答by Prabu Karana

write in your index.php codeigniter...

写在你的 index.php codeigniter ...

/*
|---------------------------------------------------------------
| TimeZone 
|---------------------------------------------------------------
|
| default Time Zone
| 

*/
if ( function_exists( 'date_default_timezone_set' ) )
date_default_timezone_set('Asia/Jakarta');

Running well in my codeigniter

在我的 codeigniter 中运行良好

回答by issacbalaji

this is the simple way to do it

这是做到这一点的简单方法

$timezone = "Asia/Calcutta";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
//echo date('d-m-Y H:i:s');

$localtime=date('H:i:s');

$sql="INSERT INTO hits (ip,edate,curtime,page_name) VALUES ('$ip', CURDATE(),'$localtime','$filename') ";

回答by bigjeff

date.timezone is intended to go in your php.ini or .htaccess file.

date.timezone 旨在进入您的 php.ini 或 .htaccess 文件。

you could do an ini_set('date.timezone', 'America/Los_Angeles');in the first line of your script and get the desired results.

您可以ini_set('date.timezone', 'America/Los_Angeles');在脚本的第一行执行一个操作并获得所需的结果。