2018-01-30 06:36 阅读 355


I'm using PHP codeigniter so here I want to get current national time. I tried NOW() in my sql code but when server change the location the server time changes. if I give date() it will take our system current time I think. in date()case if some user have some wrong date time then it will store that wrong time. So how can I solve this problem.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    duanci5913 duanci5913 2018-01-30 06:39

    Set default timezone to India in the starting of the php code


    the query

     $query = "INSERT INTO oc_customer SET date_added = ' ".date("Y-m-d H:i:s") ." ' "; 
    点赞 评论 复制链接分享
  • dp152153 dp152153 2018-01-30 06:39

    in your project index.php just put following code at line no 2


    or check timezone here

    点赞 评论 复制链接分享
  • dongzhucha3999 dongzhucha3999 2018-01-30 06:42

    Placing this date_default_timezone_set('Asia/Kolkata'); Put it in config/config.php, It will work for whole application


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

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


    Add this line inside the main index.php of codeigniter folder

    点赞 评论 复制链接分享
  • doudi4621 doudi4621 2018-01-30 06:48

    Few clarity,

    date_default_timezone_set does not have any effect on sql NOW().

    Also, server date/time does not depend on users' system unless you are fetching it in javascript.

    点赞 评论 复制链接分享
  • douciping4283 douciping4283 2018-01-30 06:54

    Here is the list of timezones PHP Provides, so eventually codeigniter does PHP Timezones

    Here is the way where you can list out timezones in CI and can use further.

    $timezones =  DateTimeZone::listIdentifiers(DateTimeZone::ALL);
     foreach ($timezones as $timezone) 
        echo $timezone;
        echo "</br>";

    Also maybe by researching I have found the way is by keeping in config.php file.

    defined('BASEPATH') OR exit('No direct script access allowed');
    点赞 评论 复制链接分享