Created
October 5, 2013 15:09
-
-
Save shortcircuit3/6842078 to your computer and use it in GitHub Desktop.
How much time has passed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prettyDate(created_at) | |
now = Time.now.to_i | |
before = Time.parse(created_at).to_i | |
diff = now - before | |
minute = 60 | |
hour = 60 * minute | |
day = 24 * hour | |
week = 7 * day | |
year = 365 * day | |
years_past = diff / year | |
weeks_past = diff / week | |
hours_past = diff / hour | |
days_past = diff / day | |
minutes_past = diff / minute | |
seconds_past = diff | |
if years_past > 0 | |
return "#{years_past}y" | |
elsif weeks_past > 0 | |
return "#{weeks_past}w" | |
elsif days_past > 0 | |
return "#{days_past}d" | |
elsif hours_past > 0 | |
return "#{hours_past}h" | |
elsif minutes_past > 0 | |
return "#{minutes_past}m" | |
else | |
return "#{seconds_past}s" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment