Created
July 26, 2011 17:15
-
-
Save shamil614/1107259 to your computer and use it in GitHub Desktop.
Twitter like relatvie time stamps (2 seconds ago, 2 minutes ago, etc) *** requires time_diff gem
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
#requires time_diff gem | |
def time_ago(time) | |
parsed_time = Time.parse(time) | |
time_diff = Time.diff(parsed_time, Time.now) | |
formated_time = "" | |
if time_diff[:day] > 0 || time_diff[:month] > 0 || time_diff[:year] > 0 | |
formated_time += "#{parsed_time.day}" if time_diff[:day] > 0 | |
formated_time += " #{parsed_time.strftime("%b")}" if time_diff[:month] > 0 | |
formated_time += " #{parsed_time.strftime("%y")}" if time_diff[:year] > 0 | |
else | |
formated_time = pluralize(time_diff[:hour], "hour") if time_diff[:hour] > 0 | |
formated_time += " " + pluralize(time_diff[:minute], "minute") if time_diff[:minute] > 0 && time_diff[:hour] == 0 | |
formated_time += " " + pluralize(time_diff[:second], "second") if time_diff[:second] > 0 && time_diff[:minute] == 0 && time_diff[:hour] == 0 | |
formated_time += " ago" | |
end | |
formated_time | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment