skip to content

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.

Comments

Yannick wrote on January 28th, 2008 @ 19:40

Nice and handy tip leading up to the 1.6.0 release. Thanks Derek.

Jon wrote on January 29th, 2008 @ 1:04

Is there a release date for 1.6 or is it still undecided?

Derek wrote on January 29th, 2008 @ 6:53

Thanks Yannick.

Jon, nothing formalized yet.  We had thought to release by now, but we’re getting lots of great community feedback, and we’re trying to get everything into there.

Alan wrote on January 30th, 2008 @ 18:15

This is great to see! By chance, will 1.6.0 allow for extending the database class as well?

Mark wrote on January 31st, 2008 @ 1:50

Not to put you down mate. But did you ever try the handy strtotime build in PHP function to convert mysql dates to timestamps :)? Works like a charm.

Alex Williams wrote on January 31st, 2008 @ 17:28

Hmmm… extending helpers could be useful… is it still possible to create your own helpers?

Also, from mysql to timestamp doesnt human_to_unix() work for that?

Alex Williams wrote on January 31st, 2008 @ 17:36

Never mind… I just answered my own question after upgrading to 1.6.0. (the answer is YES).

Can’t wait to test the new features!

Post a Comment

Sorry, comments are automatically closed after 45 days, or sooner if one entry gets targetted by spammers. Why not contact me directly?