php 在 Codeigniter 中更改时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7170746/
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-26 02:11:19  来源:igfitidea点击:

Change the time zone in Codeigniter

phpcodeigniter

提问by Ashutosh Mishra

my project is hosted on shared server and i want to change the timezone to Asia/Kolkata. i have tried setting timezone using htaccess file but failed.

我的项目托管在共享服务器上,我想将时区更改为亚洲/加尔各答。我曾尝试使用 htaccess 文件设置时区但失败了。

回答by zombat

With CodeIgniter, the best place to set the timezone is inside the main index.phpfile. It's at the same level in your project structure as the system/and application/folders.

使用 CodeIgniter,设置时区的最佳位置是在主index.php文件中。它在项目结构中与system/application/文件夹处于同一级别。

Just add the following as the first line of code in the file after the opening <?phptag:

只需将以下内容添加为文件中开始<?php标记后的第一行代码:

date_default_timezone_set('Asia/Kolkata');

That should do it for all your PHP code.

这应该适用于您的所有 PHP 代码。

Don't forget that if you're using a database, the timezone for the database will probably be different as well. If you're using MySQL, you'll want to do a SET time_zone = "+05:30"query as soon as you open a database connection.

不要忘记,如果您使用的是数据库,则数据库的时区也可能会有所不同。如果您正在使用 MySQL,您将希望在SET time_zone = "+05:30"打开数据库连接后立即执行查询。

回答by royrui

Try this

尝试这个

date_default_timezone_set('Asia/Kolkata');

in your index.php file. You don't need to have access to your php.ini file.

在您的 index.php 文件中。您不需要访问 php.ini 文件。

回答by DeathMomo

Another good practice is to put it in the CI_Controller class, right in the __construct function:

另一个好的做法是将它放在 CI_Controller 类中,就在 __construct 函数中:

public function __construct() {
    date_default_timezone_set( 'Asia/Kolkata' );
    self::$instance =& $this;
    // the rest of the code...

回答by Sahil Manchal

Do it This Way

这样做

$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
'date_default_timezone_set' => 'Asia/Kolkata'
);

回答by Ade Ayu Mahreza Yusra

date_default_timezone_set('Asia/Jakarta');

Placing this in config.php. It works!

将它放在 config.php 中。有用!

回答by Kovitsv

Please add the below code in the index.php file of the your Codeigniter project

请在您的 Codeigniter 项目的 index.php 文件中添加以下代码

 datedefaulttimezoneset(‘Asia/Kolkata'); 

Or You can also change using the php.ini file.

或者您也可以使用 php.ini 文件进行更改。

Also for more help visit http://www.tutorial-hub.com/top-interview-questions-and-answers-codeigniter-framework/

如需更多帮助,请访问http://www.tutorial-hub.com/top-interview-questions-and-answers-codeigniter-framework/

回答by Jakfar Shodiq

Yeah thanks, i try in my controller like this :

是的,谢谢,我在我的控制器中尝试这样:

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * Description of Testime
 * @class Testing
 */

class Testing extends CI_Controller {

    public function __construct() {
        parent::__construct();
        date_default_timezone_set('Asia/Jakarta');
    }

    public function index(){
        echo date('Y-m-d H:i:s');
    }
}

And works :)

和作品:)