There are many helpful modules that come with Rails. One of them is the following that can help you display the time has passed since a certain time period in the past as words in English.
All you have to do is include the module ActionView::Helpers::DateHelper and then you are able to call the method distance_of_time_in_words
Here is an example that you could use:
require 'action_view'
include ActionView::Helpers::DateHelper
def get_last_time_as_words(time)
distance_of_time_in_words(time.to_i - Time.now.to_i) + ' ago'
end
puts get_last_time_as_words(1.week.ago) # 7 days ago
puts get_last_time_as_words(2.day.ago) # 2 days ago
puts get_last_time_as_words(1.hour.ago) # about 1 hour ago
If you need to translate it to another language, then you need to translate the I18n file in the config directory that corresponds to the other languages that you want your project to be in.
You should, of course, use a locale parameter for the part ‘ ago’ in the example shown above as well.
I hope this helps you save time in trying to figure out how to do this in your projects.