Humanizing time

Posted by keruilin on Stack Overflow See other posts from Stack Overflow or by keruilin
Published on 2011-03-18T14:01:08Z Indexed on 2012/12/16 5:04 UTC
Read the original article Hit count: 99

Filed under:
|
|

I have a number of products that are perishable. Therefore, each product has an attribute called hours_expiration that tells how many hours the product can be used before it goes bad.

For ex, apple expires in 168 hours; nut expires in 4320 hours.

Given, the product's hours-to-expiration and the current time (Time.now or Date.now), how can I humanize the time-to-expiration in some of the following sample ways?

Your item is set to expire in about:

  • 6 months and 14 days
  • 1 month and 13 days
  • 1 month and 1 day
  • 27 days
  • 1 day
  • 23 hours
  • 1 hour
  • 50 minutes
  • 1 minute

Looking for something robust and simple!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby