Part of the EllisLab Network
   
 
this is how to produce a relative timeline like that of twitter in codeigniter
Posted: 12 October 2009 08:24 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  59
Joined  05-08-2008

While working on a codeigniter project with some friends i got to meet this problem of relative time. so i decided to develop one into the date helper that will ease to just auto load the date helper and then use it anywhere in my application i think i should give it back to the public. i think someone else might need it.
here are the codes, feel free to use it modify and redistribute it:

if ( ! function_exists('when'))
{
    
function when($dt,$precision=2)

    
{
    $times
=array(    365*24*60*60    => "year",
                    
30*24*60*60        => "month",
                    
7*24*60*60        => "week",
                    
24*60*60        => "day",
                    
60*60            => "hour",
                    
60                => "minute",
                    
1                => "second");

    
$passed=time()-$dt;

    if(
$passed<5)
    
{
        $output
='less than 5 seconds ago';
    
}elseif($passed 172800){
         $output
=date("jS F,Y",$dt);
    
}
    
else
    
{
        $output
=array();
        
$exit=0;

        foreach(
$times as $period=>$name)
        
{
            
if($exit>=$precision || ($exit>&& $period<60)) break;

            
$result floor($passed/$period);
            if(
$result>0)
            
{
                $output[]
=$result.' '.$name.($result==1?'':'s');
                
$passed-=$result*$period;
                
$exit++;
            
}
            
else if($exit>0$exit++;
        
}

        $output
=implode(', ',$output).' ago';
    
}

    
return $output;

you can copy and paste these codes in the date_helper found in the system/helpers folder in your project, then auto upload the date helper in your autoload.php found in the application/config folder. at your database you should use any name to hold the datetime file should have type:(int(11)) for it to work perfectly. from then things can how work well.
If any problem please do PM me.

 Signature 

Founder
http://geofeedme.com

Profile
 
 
Posted: 12 October 2009 09:17 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  278
Joined  02-17-2008

Neat, thank you!

Profile