Wednesday, May 28, 2008

Ruby: Display datetime - time in words - timeago helper function.

I found this function on the web and putted right into my helper class to display DateTime or Time into words. Displaying time in words is now more used on the web. Popular pages such as YouTube and Yahoo videos use this approach.

Copy the code and plugged into your helper class.




def timeago(time, options = {})
start_date = options.delete(:start_date) || Time.new
date_format = options.delete(:date_format) || :default
delta_minutes = (start_date.to_i - time.to_i).floor / 60
if delta_minutes.abs <= (8724*60)
distance = distance_of_time_in_words(delta_minutes)
if delta_minutes < 0
return "#{distance} from now"
else
return "#{distance} ago"
end
else
return "on #{DateTime.now.to_formatted_s(date_format)}"
end
end
def distance_of_time_in_words(minutes)
case
when minutes < 1
"less than a minute"
when minutes < 50
pluralize(minutes, "minute")
when minutes < 90
"about one hour"
when minutes < 1080
"#{(minutes / 60).round} hours"
when minutes < 1440
"one day"
when minutes < 2880
"about one day"
else
"#{(minutes / 1440).round} days"
end
end

1 comment: