如何在代码点火器中设置时区? [英] How to set time zone in code igniter?

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

问题描述

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



目前我使用这个每个条目之前 - :

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


解决方案

放置 date_default_timezone_set亚洲/加尔各答'); 上的基本url上的config.php也可以工作



PHP 支持的时区列表



application / config.php

 <?php 

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

date_default_timezone_set('Asia / Kolkata');

我发现使用完全的另一种方式是如果你想为每个用户设置一个时区

创建MY_Controller.php



在用户表中创建一个列,你可以将它命名为timezone或任何你想要的东西至。



application / core / MY_Controller.php

em>

 <?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

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

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


I working on 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');

解决方案

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

PHP List of Supported Time Zones

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

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

<?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

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

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

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

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