Viewed   92 times

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

 Answers

1

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>";
 }
Monday, September 19, 2022
3

Two ways you can do it first edit routes.php file and change 404_override to controller function this will redirect all your 404 request to that controller function

form
$route['404_override'] = 'welcome';

to
$route['404_override'] = 'ABC/index';

second option is within controller you can use _remap method/function to check either function/method exist or not. controller will be like this

class Abc extends CI_controller{

  function _remap($method_name = 'index'){

             if(!method_exists($this, $method_name)){
                $this->index();
             }
             else{
                $this->{$method_name}();
             }
         }

  public function index(){...}

  public function f1(){...} 
}
Tuesday, November 29, 2022
4

Hm, understand. As I see the subquery's source code in line 27, it wants to call _compile_select or get_compiled_select. If you can check in CI's DB_active_rec.php the _compile_select is protected so you can't access from Subquery (it isn't subclass of db).

Possible solution: _compile_select() should public or class Subquery should be extend of CI's db class. I think you should report this to author of Subquery.

Or you can extend the CI's db class :)

Sorry - I want to write it as a comment.

Sunday, December 18, 2022
 
ghazi
 
3

Try this code it might help you :

Here dashboard is the name of controller

//this will route as localhost/appFolder/admin/index
  $route['admin'] = 'dashboard'; // for your index page

//this will route as localhost/appFolder/admin/method_name
 $route['admin/(:any)'] = 'dashboard/$1';

//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';

Link the route Like

// for your index page
<a href="<?php echo base_url('admin/index'); ?>"></a>

// for your other pages
<a href="<?php echo base_url('admin/method_name'); ?>"></a>

To link the other controller defined just like

 <a href="<?php echo base_url('otherControllerName/method_name'); ?>"></a>
Sunday, September 4, 2022
 
2

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.

Saturday, December 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :