Extending CodeIgniter Helpers
January 28th, 2008
As of CodeIgniter 1.6.0 (not out as of this writing unless you use the svn repository), you’ll be able to “extend” CodeIgniter helpers. This is a huge convenience if you just need a small change, or a single additional function, but don’t want to make an entire duplicate copy of the helper.
For example, I often find myself needing a “mysqldatetime_to_timestamp()” function in there. Previously, it would mean making an entire duplicate helper in application/helpers, but now, adding an additional function is as easy as creating an application/MY_date_helper.php page, and just adding in a single function.
function mysqldatetime_to_timestamp($datetime = "")
{
// function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
$l = strlen($datetime);
if(!($l == 10 || $l == 19))
{
return false;
}
//
$date = $datetime;
$hours = 0;
$minutes = 0;
$seconds = 0;
// DATETIME only
if($l == 19)
{
list($date, $time) = explode(" ", $datetime);
list($hours, $minutes, $seconds) = explode(":", $time);
}
list($year, $month, $day) = explode("-", $date);
return mktime($hours, $minutes, $seconds, $month, $day, $year);
}
Also, if you want to change the behaviour of a current function, you can simply create an identically named function. Perfect for getting seconds out of timespan() (a personal bugaboo).
Of course, this is on top of an impressive change log of other features added into 1.6.0. The future looks very bright indeed!
* This function originally written by Clemens Kofler.
This entry was made on January 28th, 2008 @ 18:37 and filed into CodeIgniter.

Yannick wrote on January 28th, 2008 @ 19:40
Nice and handy tip leading up to the 1.6.0 release. Thanks Derek.