Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created June 6, 2013 14:35
Show Gist options
  • Save seyhunak/5721974 to your computer and use it in GitHub Desktop.
Save seyhunak/5721974 to your computer and use it in GitHub Desktop.
Ruby minutes_in_words converter
def minutes_in_words(timestamp)
timestamp = Time.at(timestamp)
minutes = (((Time.now - timestamp).abs)/60).round
return nil if minutes < 0
case minutes
when 0..1 then "less than 1 minute"
when 1..2 then "1 minute ago"
when 2..59 then "#{minutes} minutes ago"
when 60..1440 then "#{minutes/60} hours ago"
when 1440..2880 then "1 day ago"
when 2880..11519 then "#{minutes/60/24} days ago"
else "#{timestamp.strftime("%b %e %Y %R %p")}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment