Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by Bob Sawyer )

Date Helper

I added my own little function to CI’s “Date” helper file that assists with quick formatting of date strings, particularly those returned from a db table. Feel free to use it, modify it, add your own format strings, whatever.

Simple usage: include the date helper file as you normally would, then pass a date to setDate() along with a format (currently ‘short’, ‘long,’ or ‘notime’):

$this->include->helper('date');

$date "2009-06-15":
$shortdate setDate($date'short'); # returns '06 / 15 / 2009 - 9:32AM'
$longdate setDate($date'long'); # returns 'June 15, 2009 - 9:32AM'
$notime setDate($date'notime'); # returns '06 / 15 / 2009' 

Here’s the snippet:

/**
 * More simplified date stuff
 * by Bob Sawyer / Pixels and Code
 *
 * @access    public
 * @param    string
 * @param    integer
 * @return    integer
 */    
if ( ! function_exists('setDate'))
{
    
function setDate($datestr '',$format 'long')
    
{
        
if ($datestr == '')
            return 
'--';
    
        
$time strtotime($datestr);
        switch (
$format{
            
case 'short'$fmt 'm / d / Y - g:iA'; break;
            case 
'long'$fmt 'F j, Y - g:iA'; break;
            case 
'notime'$fmt 'm / d / Y'; break;
        
}
        $newdate 
date($fmt,$time);
        return 
$newdate;
    
}