If I have a time variable in Ruby, how could I say that it refers to an event that happened one of the following:
"x minutes ago" or "x hours ago" or "x days ago"
Obviously if something happened 2 days ago, I would not want to say it happened such-and-such minutes ago.
You need to do something like this:
Here is a complete example if you choose to go outside of full Rails but are willing to use active_support (
gem install actionview
)At time of writing, this outputs:
If you are on rails:
Here's the language agnostic version which you should be able to convert into any language:
Note that the year/month figures are approximate (based on averages) but that shouldn't really matter since the relative error will still be very low.