Skip to content

Instantly share code, notes, and snippets.

@amiel
Created February 25, 2009 01:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save amiel/69934 to your computer and use it in GitHub Desktop.
give it a value in seconds, and it will return a sentence
def length_of_time_in_words(seconds)
seconds = seconds.to_i
%w[ year month week day hour minute second ].collect do |unit|
unit_in_seconds = 1.send(unit).to_i
n = seconds / unit_in_seconds
seconds -= n * unit_in_seconds
pluralize(n, unit) unless n.zero?
end.compact.to_sentence
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment