How to set time zone in codeigniter?
PhpCodeigniterPhp Problem Overview
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');
Php Solutions
Solution 1 - Php
Placing this date_default_timezone_set('Asia/Kolkata');
on config.php above base url also works
PHP List of Supported Time Zones
application/config/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>";
}
Solution 2 - Php
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';
Solution 3 - Php
Add this line inside the main index.php
of codeigniter folder
date_default_timezone_set('Asia/Kolkata');
Solution 4 - Php
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.
Solution 5 - Php
Add this line to autoload.php in the application folder:
$autoload['time_zone'] = date_default_timezone_set('Asia/Kolkata');
Solution 6 - Php
As describe here
-
Open your
php.ini
file (look for it) -
Add the following line of code on the top of the file:
date.timezone = "US/Central"
-
Verify the changes by going to
phpinfo.php
Solution 7 - Php
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');
}
Solution 8 - Php
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');
Solution 9 - Php
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';
Solution 10 - Php
Put it in config/config.php
, It will work for whole application or index.php
of codeigniter.
Solution 11 - Php
You can find 'time_zone' string in application/autoload.php
$autoload['time_zone'] = date_default_timezone_set('America/Toronto');
This works for me. Thanks.
Solution 12 - Php
Add it to your project/index.php
file, and it will work on all over your site.
> date_default_timezone_set('Asia/kabul');
Solution 13 - Php
How to set default time zone in CodeIgniter 4
browse to and edit
/app/Config/App.php
at line #103 you'll find
public $appTimezone = 'America/Chicago';
which you can change to any time zone available in the List of Supported Timezones
Solution 14 - Php
in CI4, you can use .env file (manually created) in project root. refer Codeigniter4 environement variables
app.appTimezone = "Asia/Kolkata"