Ashutosh Mishra
Ashutosh Mishra

Reputation: 407

Change the time zone in Codeigniter

my project is hosted on shared server and i want to change the timezone to Asia/Kolkata. i have tried setting timezone using htaccess file but failed.

Upvotes: 14

Views: 48577

Answers (7)

Sahil Manchal
Sahil Manchal

Reputation: 468

Do it This Way

$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
'date_default_timezone_set' => 'Asia/Kolkata'
);

Upvotes: 1

date_default_timezone_set('Asia/Jakarta');

Placing this in config.php. It works!

Upvotes: 0

Sandeep Kumawat
Sandeep Kumawat

Reputation: 81

Please add the below code in the index.php file of the your Codeigniter project

 datedefaulttimezoneset(‘Asia/Kolkata’); 

Or You can also change using the php.ini file.

Also for more help visit http://www.tutorial-hub.com/top-interview-questions-and-answers-codeigniter-framework/

Upvotes: 0

JorgeRenteral
JorgeRenteral

Reputation: 176

Another good practice is to put it in the CI_Controller class, right in the __construct function:

public function __construct() {
    date_default_timezone_set( 'Asia/Kolkata' );
    self::$instance =& $this;
    // the rest of the code...

Upvotes: 1

Jakfar Shodiq
Jakfar Shodiq

Reputation: 59

Yeah thanks, i try in my controller like this :

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * Description of Testime
 * @class Testing
 */

class Testing extends CI_Controller {

    public function __construct() {
        parent::__construct();
        date_default_timezone_set('Asia/Jakarta');
    }

    public function index(){
        echo date('Y-m-d H:i:s');
    }
}

And works :)

Upvotes: 0

royrui
royrui

Reputation: 1207

Try this

date_default_timezone_set('Asia/Kolkata');

in your index.php file. You don't need to have access to your php.ini file.

Upvotes: 5

zombat
zombat

Reputation: 94197

With CodeIgniter, the best place to set the timezone is inside the main index.php file. It's at the same level in your project structure as the system/ and application/ folders.

Just add the following as the first line of code in the file after the opening <?php tag:

date_default_timezone_set('Asia/Kolkata');

That should do it for all your PHP code.

Don't forget that if you're using a database, the timezone for the database will probably be different as well. If you're using MySQL, you'll want to do a SET time_zone = "+05:30" query as soon as you open a database connection.

Upvotes: 40

Related Questions