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 date_default_timezone_set() function is used to sets the default timezone used by all date or time functions. In CodeIgniter, the best place to set the timezone is index. php file of application root. Place the date_default_timezone_set() with your desired timezone in the main index.
php use CodeIgniter\I18n\Time; $myTime = new Time('+3 week'); $myTime = new Time('now'); You can pass in strings representing the timezone and the locale in the second and parameters, respectively. Timezones can be any supported by PHP's DateTimeZone class. The locale can be any supported by PHP's Locale class.
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>"; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With