Created
August 18, 2008 15:51
-
-
Save technicalpickles/5994 to your computer and use it in GitHub Desktop.
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 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