How to Set Time Zone in Codeigniter

How to set time zone in codeigniter?

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>";
}

Change the time zone in Codeigniter

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.

Codeigniter Default Timezone Issue

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

i.e., first setting the timezone as per the server, retrieve the data and convert it to display

Thanks to gzipp as mentioned in this question

How to set date.timezone for CodeIgniter to work with php 5.3

If you Googled "CodeIgniter PHP 5.3" you would have found this article pretty quickly :)

http://philsturgeon.co.uk/blog/2009/12/CodeIgniter-on-PHP-5.3

To fix this, you only need to edit the main index.php for your CodeIgniter application:

if( ! ini_get('date.timezone') )
{
date_default_timezone_set('GMT');
}

This modification is something you will probably need to make for any CodeIgniter application running on PHP 5.3 and can easily be modified to your local timezone. There is a full list of supported timezones in the PHP manual here.

PHP, Codeigniter: How to Set Date/Time based on users timezone/location globally in a web app?

I think the easiest way is define a timezone for internal data storage (your server's time zone or UTC) and convert the time according to the user's time zone when outputting it.

I don't know CodeIgniter so I can't point you to the right functions. One prominent library that is timezone aware is Zend_Date: Working with Timezones I have not worked with these functions yet but they look promising.

However, once you know the user's time zone, it's not difficult to put together an own function that adds/substracts the offset when outputting dates and/or times.

Possibly related question:

  • MySQL: keep server timezone or user timezone?


Related Topics



Leave a reply



Submit