简体   繁体   中英

Codeigniter Default Timezone Issue

Brief: Am working on a CodeIgniter3.0.3 version application. It is a multi-tenant application. Single code base connects to multiple databases based on the user login (interacts with different database). Each user could be from different timezone across the globe. I am trying to apply the below code.

date_default_timezone_set("Africa/Accra");
$time =  Date('Y-m-d h:i:s');echo $time;
$expiry=$this->db->get_where('settings', array('type' => 'registration_date'))->row()->description;
$timestamp=strtotime($expiry);
echo Date('Y-m-d h:i:s',$timestamp);

The second line of this code shows the time properly accordingly, whereas the last line is showing the server time.

The description column of the setting table is of varchar type which stored value like "2016-02-08 16:29:09".

Also, if I add date_default_timezone_set("Africa/Accra") just before the last line, its working fine.

Question : I have tried setting this property in index.php, config.php, controller construct method, created helper library, but none of them seems to have worked. How can I resolve it please?

Edit : The date value stored in the DB was before applying this timezone_set. There was no offset in the DB before.

As of now I don't know the timezone for each of the users. I can set it to GMT and update all the columns accordingly too. Do I need to update all the existing date columns based on the timezone set? But, What will happen if any user changes their timezone prefer? Is there any other workaround to this?

place

date_default_timezone_set("Africa/Accra");

in index.php file

The answer from Dhruv helped to some extent. But displaying previously stored dates (before implementing the timezone) was the issue. Then followed up several SO articles arrived the solution like below.

date_default_timezone_set("Asia/Kolkata"); 
$registered=$this->db->get_where('settings', array('type' => 'registration_date'))->row()->description;
$dt_obj = new DateTime($registered);
$dt_obj->setTimezone(new DateTimeZone('Africa/Accra'));
echo $dt_obj->format('Y-m-d H:i:s');

ie, first setting the timezone as per the server, retrieve the data and convert it to display

Thanks to gzipp as mentioned in this question

In config.php, add/modify the following block:

/*
|--------------------------------------------------------------------------
| Master Time Reference
|--------------------------------------------------------------------------
|
| Options are 'local' or any PHP supported timezone. This preference tells
| the system whether to use your server's local time as the master 'now'
| reference, or convert it to the configured one timezone. See the 'date
| helper' page of the user guide for information regarding date handling.
|
*/
$config['time_reference'] = 'local';

Refer to this guide for more information.

Hopefully that will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM