[php] How to set time zone in codeigniter?

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');

This question is related to php codeigniter

The answer is


If you are looking globabally setup timezone in whole project then you can setup it CI application/config.php file

$config['time_reference'] = 'Asia/Dubai';

Put it in config/config.php, It will work for whole application or index.php of codeigniter.


add it in your index.php file, and it will work on all over your site

if ( function_exists( 'date_default_timezone_set' ) ) {
    date_default_timezone_set('Asia/Kolkata');
}

you can try this:

date_default_timezone_set('Asia/Kolkata');

In application/config.php OR application/autoload.php

There is look like this:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

date_default_timezone_set('Asia/Kolkata');

It's working fine for me, i think this is the best way to define DEFAULT TIMEZONE in codeignitor.


In the application/config folder, get the file config.php and check for the below:

$config['time_reference'] = '';

Change the value to your preferred time zone. For example to set time zone to Nairobi Kenya: $config['time_reference'] = 'Africa/Nairobi';


As describe here

  1. Open your php.ini file (look for it)

  2. Add the following line of code on the top of the file:

    date.timezone = "US/Central"

  3. Verify the changes by going to phpinfo.php


Add it to your project/index.php file, and it will work on all over your site.

date_default_timezone_set('Asia/kabul');


Add it to your project/application/config/config.php file, and it will work on all over your site.

date_default_timezone_set('Asia/Kolkata');

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

date_default_timezone_set('Asia/Kolkata');

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

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