Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Created August 18, 2008 15:51
Show Gist options
  • Save technicalpickles/5994 to your computer and use it in GitHub Desktop.
Save technicalpickles/5994 to your computer and use it in GitHub Desktop.
def exact_distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_seconds = ((to_time - from_time).abs).round
distance_in_minutes = (distance_in_seconds / 60).round
distance_in_hours = (distance_in_minutes / 60).round
distance_in_hours
days = (distance_in_hours / 24).round
hours = (distance_in_minutes % 24).round
minutes = (distance_in_seconds % 60).round
seconds = distance_in_seconds % 60
array = [] # i hate myself for naming it this
array << "#{days} days" if days > 0
array << "#{hours} hours" if hours > 0
array << "#{minutes} minutes" if minutes > 0
# array << "#{seconds} seconds" if seconds > 0
array.join(', ')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment