[eluser]ebot[/eluser]
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:
Code:
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>0 && $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
![Sad Sad](https://forum.codeigniter.com/images/smilies/sad.png)
int(11)) for it to work perfectly. from then things can how work well.
If any problem please do PM me.