[eluser]jedd[/eluser]
In my views I make a call to a function in my helper:
Code:
echo pdb_rough_time_ago (mysql_to_unix($msgdate)) . " ago";
$msgdate is in mysql format, of course. One day I'll write my helper function to be a bit more adept at recognising and coping with different date formats.
My helper contains:
Code:
/**
* shows pretty rough 'time ago' string
*
* @param string unix date stamp
* @return string
*/
function pdb_rough_time_ago ($date) {
/// @TODO cope with non-unix date stamps
/// @TODO cope with multiple parameters for showing arbitrary diff-between two dates
/// @TODO get more sophisticated - if it's > 10 hours, don't show minutes .. that kind of thing
$difference = timespan ($date);
// If it's only minutes, or hours + minutes, don't mess with it,
// otherwise just show two most significant components.
if ( substr_count($difference, ",") > 1) {
$date_array = explode (", ", $difference);
$difference = $date_array[0] .", ". $date_array[1];
}
return $difference;
}
While this diverges slightly from the original question - it's a 'rough time' example after all - it gives an idea of the benefits of using [url="http://ellislab.com/codeigniter/user-guide/helpers/date_helper.html"]CI's date helper[/url] (which is kind of the point isn't it)?