Snowfish
Snowfish

Reputation: 7747

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

When date.timezone in php.ini is commented out, it gives me:

A PHP Error was encountered

Severity: Warning

Message: main(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for '-8.0/no DST' instead

Filename: controllers/helloworld.php

Line Number: 2

When I have

date.timezone = "America/Los_Angeles"

It gives me this:

Server error The website encountered an error while retrieving http://localhost/ci/index.php/helloworld. It may be down for maintenance or configured incorrectly. Here are some suggestions: Reload this web page later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

I am using php 5.3, CodeIgniter 2.0.0, and Apache 2.2.

Update 1: I tried loading a test.php without CodeIgniter, where the first 3 lines of test.php is

date_default_timezone_set('America/Los_Angeles');
echo date("l j \of F Y h:i:s A");

And it works fine, different timezones also works fine too. So I suspect the problem is from CodeIgniter.

Upvotes: 27

Views: 88585

Answers (6)

Maruf Sheikh
Maruf Sheikh

Reputation: 1

edit your config.php file from

    $config['time_reference'] = 'local';

to

    $config['time_reference'] = 'UTC';

Upvotes: 0

Prabu Karana
Prabu Karana

Reputation: 302

write in your index.php codeigniter...

/*
|---------------------------------------------------------------
| TimeZone 
|---------------------------------------------------------------
|
| default Time Zone
| 

*/
if ( function_exists( 'date_default_timezone_set' ) )
date_default_timezone_set('Asia/Jakarta');

Running well in my codeigniter

Upvotes: 8

issacbalaji
issacbalaji

Reputation: 41

this is the simple way to do it

$timezone = "Asia/Calcutta";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
//echo date('d-m-Y H:i:s');

$localtime=date('H:i:s');

$sql="INSERT INTO hits (ip,edate,curtime,page_name) VALUES ('$ip', CURDATE(),'$localtime','$filename') ";

Upvotes: 4

Mavelo
Mavelo

Reputation: 1259

Yes, if you cannot directly edit the php.ini file, placing...

ini_set('date.timezone', 'America/New_York');

...as the first line in CI's index.php works fine.

Reference: PHP's Available Timezones

Upvotes: 21

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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.

Upvotes: 76

bigjeff
bigjeff

Reputation: 81

date.timezone is intended to go in your php.ini or .htaccess file.

you could do an ini_set('date.timezone', 'America/Los_Angeles'); in the first line of your script and get the desired results.

Upvotes: 3

Related Questions