php 如何在codeigniter中设置时区?

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

How to set time zone in codeigniter?

phpcodeigniter

提问by Vipul sharma

I am working in a php project using codeigniter. Please advise me what is the global way to set time zone for php and mysql . In which file I can set this. I want to set it without php.ini and .htaccess file.

我正在使用 codeigniter 的 php 项目中工作。请告诉我为 php 和 mysql 设置时区的全局方式是什么。我可以在哪个文件中设置它。我想在没有 php.ini 和 .htaccess 文件的情况下设置它。

currently I am using this before every entry -:

目前我在每次进入之前都使用它 -:

date_default_timezone_set("Asia/Kolkata");
$time =  Date('Y-m-d h:i:s');

回答by Mr. ED

Placing this date_default_timezone_set('Asia/Kolkata');on config.php above base url also works

将它date_default_timezone_set('Asia/Kolkata');放在 config.php 上面的基本 url 上也有效

PHP List of Supported Time Zones

PHP支持的时区列表

application/config.php

应用程序/config.php

<?php

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

date_default_timezone_set('Asia/Kolkata');

Another way I have found use full is if you wish to set a time zone for each user

我发现使用 full 的另一种方法是,如果您希望为每个用户设置一个时区

Create a MY_Controller.php

创建一个 MY_Controller.php

create a column in your user table you can name it timezone or any thing you want to. So that way when user selects his time zone it can can be set to his timezone when login.

在您的用户表中创建一列,您可以将其命名为时区或任何您想要的名称。这样当用户选择他的时区时,它可以在登录时设置为他的时区。

application/core/MY_Controller.php

应用程序/核心/MY_Controller.php

<?php

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->set_timezone();
    }

    public function set_timezone() {
        if ($this->session->userdata('user_id')) {
            $this->db->select('timezone');
            $this->db->from($this->db->dbprefix . 'user');
            $this->db->where('user_id', $this->session->userdata('user_id'));
            $query = $this->db->get();
            if ($query->num_rows() > 0) {
                date_default_timezone_set($query->row()->timezone);
            } else {
                return false;
            }
        }
    }
}

Also to get the list of time zones in php

还可以获取 php 中的时区列表

 $timezones =  DateTimeZone::listIdentifiers(DateTimeZone::ALL);

 foreach ($timezones as $timezone) 
 {
    echo $timezone;
    echo "</br>";
 }

回答by Girish

If you are looking globabally setup timezone in whole project then you can setup it CI application/config.phpfile

如果您正在整个项目中寻找全局设置时区,那么您可以设置它的 CIapplication/config.php文件

$config['time_reference'] = 'Asia/Dubai';

回答by Saty

Add this line inside the main index.phpof codeigniter folder

将此行添加index.php到 codeigniter 文件夹的主目录中

date_default_timezone_set('Asia/Kolkata');

回答by kishan Radadiya

you can try this:

你可以试试这个:

date_default_timezone_set('Asia/Kolkata');

In application/config.phpOR application/autoload.php

application/config.phpapplication/autoload.php

There is look like this:

有这样的:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

date_default_timezone_set('Asia/Kolkata');

It's working fine for me, i think this is the best way to define DEFAULT TIMEZONE in codeignitor.

它对我来说很好用,我认为这是在 codeignitor 中定义 DEFAULT TIMEZONE 的最佳方式

回答by Praveen Kumar Sharma

Add this line to autoload.php in the application folder:

将此行添加到应用程序文件夹中的 autoload.php 中:

$autoload['time_zone'] = date_default_timezone_set('Asia/Kolkata');

回答by theGreenCabbage

As describe here

作为描述在这里

  1. Open your php.inifile (look for it)

  2. Add the following line of code on the top of the file:

    date.timezone = "US/Central"

  3. Verify the changes by going to phpinfo.php

  1. 打开你的php.ini文件(寻找它)

  2. 在文件顶部添加以下代码行:

    date.timezone = "US/Central"

  3. 通过转到验证更改 phpinfo.php

回答by Sudhir Bastakoti

add it in your index.phpfile, and it will work on all over your site

将它添加到您的index.php文件中,它将在您的整个站点上运行

if ( function_exists( 'date_default_timezone_set' ) ) {
    date_default_timezone_set('Asia/Kolkata');
}

回答by shankar kumar

Put it in config/config.php, It will work for whole application or index.phpof codeigniter.

把它放进去config/config.php,它将适用于整个应用程序或index.phpcodeigniter。

回答by Nijas

Add it to your project/application/config/config.php file, and it will work on all over your site.

将它添加到您的 project/application/config/config.php 文件中,它将在您的整个站点上运行。

date_default_timezone_set('Asia/Kolkata');

回答by ROK

In the application/config folder, get the file config.php and check for the below:

在 application/config 文件夹中,获取文件 config.php 并检查以下内容:

$config['time_reference'] = '';

$config['time_reference'] = '';

Change the value to your preferred time zone. For example to set time zone to Nairobi Kenya: $config['time_reference'] = 'Africa/Nairobi';

将值更改为您的首选时区。例如将时区设置为肯尼亚内罗毕: $config['time_reference'] = 'Africa/Nairobi';