Skip to content

Instantly share code, notes, and snippets.

@radavis
Created June 6, 2017 00:00
Show Gist options
  • Save radavis/4a775fd0298340e43433b219f49b8529 to your computer and use it in GitHub Desktop.
Save radavis/4a775fd0298340e43433b219f49b8529 to your computer and use it in GitHub Desktop.
module TimeHelper
def now
Time.new.to_i
end
def time_ago(seconds, now = Time.now.to_i)
minutes = (now - seconds) / 60
quantity = [minutes / 525600, minutes / 43200, minutes / 1440, minutes / 60, minutes]
%w(year month day hour minute).each_with_index do |type, i|
q = quantity[i]
if type == "minute" && q < 2
return "just now"
elsif q > 0
return "#{q} #{type}#{ q == 1 ? "" : "s" } ago"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment