Created
March 3, 2015 15:39
-
-
Save masonfmatthews/91e46d2eafb7a2c23062 to your computer and use it in GitHub Desktop.
Time Passed Methods (requires Rails to run)
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 time_passed(updated_at) | |
difference = (Time.now - updated_at.to_time) | |
time = (difference / 1.day).round | |
units = "day" | |
if time == 0 | |
time = (difference / 1.hour).round | |
units = "hour" | |
if time == 0 | |
time = (difference / 1.minute).round | |
units = "minute" | |
end | |
end | |
"Updated #{time} #{units}#{time==1 ? '' : 's'} ago" | |
end | |
def time_passed(updated_at) | |
difference = Time.now - updated_at.to_time | |
[:day, :hour, :minute].each do |unit| | |
if difference >= 1.send(unit) | |
time = (difference / 1.send(unit)).round | |
return "Updated #{time} #{unit}#{time==1 ? '' : 's'} ago" | |
end | |
end | |
return "Updated #{difference} seconds ago" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment