如何在Codeigniter中设置时区? [英] How to set time zone in codeigniter?

查看:119
本文介绍了如何在Codeigniter中设置时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用codeigniter在php项目中工作.请告诉我为php和mysql设置时区的全局方法是什么.我可以在哪个文件中进行设置.我想将其设置为不包含php.ini和.htaccess文件.

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.

当前,我在每次输入-之前使用它-

currently I am using this before every entry -:

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

推荐答案

将此date_default_timezone_set('Asia/Kolkata');放置在基本URL上方的config.php上

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

PHP 支持的时区列表

application/config.php

application/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

创建一个MY_Controller.php

Create a 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

application/core/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;
            }
        }
    }
}

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

Also to get the list of time zones in php

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

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

这篇关于如何在Codeigniter中设置时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆