Skip to content

Instantly share code, notes, and snippets.

@spacecowb0y
Created April 7, 2011 19:18
Show Gist options
  • Save spacecowb0y/908484 to your computer and use it in GitHub Desktop.
Save spacecowb0y/908484 to your computer and use it in GitHub Desktop.
Time in Words - Ruby Helper
def time_ago_in_words(timestamp)
minutes = (((Time.now - timestamp).abs)/60).round
return nil if minutes < 0
case
when minutes < 1 then "less than a minute ago"
when minutes == 1 then "1 minute ago"
when minutes < 50 then "#{minutes} minutes ago"
when minutes < 90 then "1 hour ago"
when minutes < 1440 then "#{(minutes / 60).round} hours ago"
else
timestamp.strftime('%d %b %Y')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment