Skip to content

Instantly share code, notes, and snippets.

@masonfmatthews
Created March 3, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masonfmatthews/91e46d2eafb7a2c23062 to your computer and use it in GitHub Desktop.
Save masonfmatthews/91e46d2eafb7a2c23062 to your computer and use it in GitHub Desktop.
Time Passed Methods (requires Rails to run)
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