59

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');
Fawzan
  • 4,738
  • 8
  • 41
  • 85
Vipul sharma
  • 1,245
  • 1
  • 13
  • 33
  • 1
    Pretty sure in `php.ini` you can set the global time zone. – theGreenCabbage Jul 09 '15 at 06:19
  • Probably [this](http://stackoverflow.com/questions/4882790/how-to-set-date-timezone-for-code-igniter-to-work-with-php5-3) and [this](http://stackoverflow.com/questions/17426887/codeigniter-timezone-mysql-settings) can help you. – Rene Korss Jul 09 '15 at 06:22
  • possible duplicate of [Change the time zone in Codeigniter](http://stackoverflow.com/questions/7170746/change-the-time-zone-in-codeigniter) –  Jul 09 '15 at 06:31

14 Answers14

110

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>";
 }
Akshay I
  • 3,675
  • 1
  • 34
  • 57
  • 2
    This won't work on some stupid shared hosting because they disable it so user can not change timezone using *date_default_timezone_set* and to change it you have to upgrade your hosting plan which will be expensive. Be careful whose hosting you buy it may backfire. – Faizan Anwer Ali Rupani Dec 11 '17 at 12:29
  • Thanks it works for me as adding `date_default_timezone_set('Asia/Kolkata');` after the base_path definition – seedme Mar 26 '21 at 10:26
14

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';
Girish
  • 11,907
  • 3
  • 34
  • 51
  • there are 3 Config.php files in the CI 4 structure and there is no path `/application` . Which one do you kindly mean? – Robert Mar 12 '21 at 05:50
9

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

date_default_timezone_set('Asia/Kolkata');
Saty
  • 22,443
  • 7
  • 33
  • 51
4

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');
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
4

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.

kishan Radadiya
  • 788
  • 6
  • 14
  • In **Bluehost** i tried in **application/autoload.php** and it worked for me, i´ve tried in **config.php** but didn't work. This `date_default_timezone_set('Asia/Kolkata);` – borissoto Apr 17 '21 at 05:29
4

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

$autoload['time_zone'] = date_default_timezone_set('Asia/Kolkata');
Floern
  • 33,559
  • 24
  • 104
  • 119
  • In **Bluehost** web host, i tried in **application/autoload.php** and it worked for me, This `date_default_timezone_set('Asia/Kolkata);` – borissoto Apr 17 '21 at 05:30
3

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

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
2

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');
perror
  • 7,071
  • 16
  • 58
  • 85
Nijas
  • 21
  • 1
2

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

ROK
  • 21
  • 3
2

in CI4, you can use .env file (manually created) in project root. refer Codeigniter4 environement variables

app.appTimezone = "Asia/Kolkata"
smk
  • 301
  • 6
  • 13
1

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

Bruce
  • 1,647
  • 4
  • 20
  • 22
shankar kumar
  • 648
  • 5
  • 9
1

You can find 'time_zone' string in application/autoload.php

$autoload['time_zone'] = date_default_timezone_set('America/Toronto'); 

This works for me. Thanks.

0

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

date_default_timezone_set('Asia/kabul');

Anbuselvan Rocky
  • 606
  • 6
  • 22
hedayat
  • 1
  • 2
  • 4
    Some slight editing on your answer should make it easier to read and understand. I think you are trying to say "Add `date_default_timezone_set('Asia/kabul');` to your `project/index.php` file." Please use code-tags where necessary, it makes the post easier to read. In this instance, can you also please explain why "'Asia/kabul" instead of "Asia/Kolkata" - is there any significance in the change? – AJD Apr 01 '18 at 06:57
0

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

http://php.net/manual/en/timezones.php

Robert
  • 490
  • 1
  • 5
  • 17