CodeIgniter on PHP 5.3

Posted: 2009-12-10
Category: CodeIgniter

Last night I set up Zend Community Server with PHP 5.3 and gave PyroCMS (running CodeIgniter 1.7.2) a spin. Out of the box v0.9.7.3 and v0.9.8-dev seemed to work fine, except for a single Warning at the top of each page:

"warning: date(): 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."

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

/*
|---------------------------------------------------------------
| DEFAULT TIMEZONE
|---------------------------------------------------------------
|
| Set the default timezone for date/time functions to use if
| none is set on the server.
|
*/

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.

Another tweak that might be worth trying (although not something I had an issue with on my apps) will be removing error messages for deprecated functions. I personally would prefer to see these so I know what to fix, but if you are trying to fix an app running on PHP 5.3 quickly, change your error reporting level in index.php like so:

/*
|---------------------------------------------------------------
| PHP ERROR REPORTING LEVEL
|---------------------------------------------------------------
|
| By default CI runs with error reporting set to ALL. For security
| reasons you are encouraged to change this when your site goes live.
| For more info visit: http://www.php.net/error_reporting
|
*/

error_reporting(E_ALL & ~E_DEPRECATED);

Have you noticed any other problems when upgrading your CodeIgniter applications to PHP 5.3? If so (and the damn captchas are actually working...) please let me know in the comments.

Comments

Gravatar
Ben Edmunds

2009-12-10

Good post Phil. I just ran across the timezone problem earlier this week.

This will also be a good post to point all the "AHHH MY CI IS BROKEN IN PHP5.3!" CI forum posters to.


Also, this captcha is wicked small; I'm going to have to zoom in...

Thanks,

Gravatar
Nicolas Bui

2009-12-12

It would be quicker and cleaner to edit the php.ini file directly instead...

Gravatar

2009-12-14

@Nicolas Bui: Of course, but if you have no access to the php.ini it could be an issue.

That aside, I try to make my applications as portable as humanly possible. If someone downloads my application and see's an error, they assume the application is at fault and not their server. While it would be ideal if I could just say "just sort your server out" it's much easier to do things this way.

Gravatar
Jason

2010-04-02

Thanks, this definitely solved my problem really quickly...

Gravatar
Bolton Li

2010-10-09

Thanks, It helped me a lot:D

Gravatar
Nirmal

2010-12-27

This solution did not work for me. I tried an alternate solution and that fixed the thing.
You need to simply put below line in your php.ini and restart the webserver.

date.timezone = "Asia/Kolkata";

Depending on your timezone you can modify it as suits you.

Enjoy!

Gravatar

2010-12-31

@Nirmal This solution works for installations that do not have access to their php.ini file (shared hosts and the like). If you have access to your ini file then you don't need to worry about it.

Gravatar
Joaz

2011-01-25

Thanks, it worked for me!

Gravatar
Manuel Guerrero

2011-07-23

Thanks, solved my problem without any inconvenience

Gravatar
Steve

2011-07-25

This worked like a charm. Thanks for the info!

Gravatar
Gabor

2011-07-29

Thanks a lot man, worked fine !

Gravatar
Abu Najiyah

2011-09-19

Thanks, nice tutor

Gravatar
Pablo Digiani

2012-04-26

Thanks Phill! You saved me a lot of time this morning!

Gravatar
Radenko

2012-06-06

Thanks, but it didn't help until I put this in the View file:

putenv("TZ=Europe/Berlin");

Posting comments after three months has been disabled.