代码之家  ›  专栏  ›  技术社区  ›  Rajesh

Codeigniter默认时区问题

  •  2
  • Rajesh  · 技术社区  · 10 年前

    简介:我正在开发CodeIgniter3.0.3版本的应用程序。它是一个多租户应用程序。单个代码库基于用户登录连接到多个数据库(与不同的数据库交互)。每个用户可能来自全球不同的时区。我正在尝试应用以下代码。

    date_default_timezone_set("Africa/Accra");
    $time =  Date('Y-m-d h:i:s');echo $time;
    $expiry=$this->db->get_where('settings', array('type' => 'registration_date'))->row()->description;
    $timestamp=strtotime($expiry);
    echo Date('Y-m-d h:i:s',$timestamp);
    

    该代码的第二行相应地显示了时间,而最后一行显示了服务器时间。

    设置表的描述栏为varchar类型,存储值为“2016-02-08 16:29:09”。

    此外,如果我在最后一行之前添加date_default_timezone_set(“Africa/Accra”),它可以正常工作。

    问题 :我已尝试在索引中设置此属性。php,配置。php、controller-construct方法、创建了helper库,但它们似乎都不起作用。我该如何解决?

    编辑 :数据库中存储的日期值在应用此timezone_set之前。以前DB中没有偏移。

    到目前为止,我还不知道每个用户的时区。我可以将其设置为GMT,并相应地更新所有列。 是否需要根据时区设置更新所有现有的日期列?但是,如果任何用户更改他们的时区偏好,会发生什么?对此还有其他解决方法吗?

    3 回复  |  直到 10 年前
        1
  •  2
  •   dhruv jadia    10 年前

    位置

    date_default_timezone_set("Africa/Accra");
    

    在索引中。php文件

        2
  •  1
  •   Community Mohan Dere    9 年前

    德鲁夫的回答在一定程度上有所帮助。但显示以前存储的日期(在实现时区之前)是个问题。随后跟进了几篇SO文章,得出了如下的解决方案。

    date_default_timezone_set("Asia/Kolkata"); 
    $registered=$this->db->get_where('settings', array('type' => 'registration_date'))->row()->description;
    $dt_obj = new DateTime($registered);
    $dt_obj->setTimezone(new DateTimeZone('Africa/Accra'));
    echo $dt_obj->format('Y-m-d H:i:s');
    

    i、 e.首先根据服务器设置时区,检索数据并将其转换为显示

    感谢本文中提到的gzipp question

        3
  •  0
  •   Sᴀᴍ Onᴇᴌᴀ    9 年前

    在配置中。php,添加/修改以下块:

    /*
    |--------------------------------------------------------------------------
    | Master Time Reference
    |--------------------------------------------------------------------------
    |
    | Options are 'local' or any PHP supported timezone. This preference tells
    | the system whether to use your server's local time as the master 'now'
    | reference, or convert it to the configured one timezone. See the 'date
    | helper' page of the user guide for information regarding date handling.
    |
    */
    $config['time_reference'] = 'local';
    

    提到 this guide 了解更多信息。

    希望这会对你有所帮助。