繁体   English   中英

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

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

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

遇到 PHP 错误

严重性:警告

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

文件名:控制器/helloworld.php

行号:2

当我有

date.timezone = "America/Los_Angeles"

它给了我这个:

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

我正在使用 php 5.3、CodeIgniter 2.0.0 和 Apache 2.2。

更新 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");

它运行良好,不同的时区也运行良好。 所以我怀疑问题出在 CodeIgniter 上。

如果您使用Google搜索“CodeIgniter PHP 5.3”,您会很快发现这篇文章:)

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

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

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

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

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

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

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

参考: PHP的可用时区

写在你的index.php codeigniter中......

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

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

在我的codeigniter中运行良好

这是一种简单的方法

$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') ";

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

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

编辑您的 config.php 文件

    $config['time_reference'] = 'local';

    $config['time_reference'] = 'UTC';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM