Youtilize

Read all about technology, web development and creative entrepreneurship

PHP's time_since equivalent in Ruby on Rails

Having switched to Ruby on Rails (for now), I was looking for the same functionality as the popular time_since() function and after hours of searching, I found that Ruby has this exact function built in.

No need to download a 3rd party module and add it to your project, simply use:

time_ago_in_words()

time_ago_in_words() does exactly this. Simply pass it a Date or a Time object and it will spit out time since in words. API

distance_of_time_in_words()

This is another cool function I just added to the comment bit. Instead of simply saying how long has passed since comment was added to now, I wanted to say how long has passed after the post was created to when the comment was added. Ex: about 10 minutes after. API

Here’s my code in the post view:

<% for comment in @post.comments %>
  ... stuff here ...
  <%= distance_of_time_in_words(@post.added, comment.added) %>
  ... stuff here ...
<% end %>

Hope these help out. I spend ridiculous amount of time looking for functions like this so you won’t have to :)

4 Comments

Patrick
about 1 year ago

Dimitry,

I love your site design and layout. Haha, I didn’t even need to read the content before I subscribed to your posts. Looking forward to reading your stuff. Also, that link you left on Okdork about the new note sharing application is great. I can’t wait till they’ve got it working.

Dimitry
about 1 year ago

Thanks Patrick! That’s always good to hear.

As far as that note sharing app., I love the idea as I have a ton of school notes/papers/etc just lying around and I’m just one person. Imagine combining thousands of students’ notes.

Something as simple as a school Wiki can do the job with file attachments for each topic.

Now that we’re so far off-topic, I can share the link in question: http://www.centernetworks.com/and-the-winner-is-the-note-sharing-tool

Joel
about 1 year ago

Dimitry: One big issue that will inevitably be faced with a company acting as the sharing intermediary will be the IP covering the notes themselves (read up on the ownership of “your papers” once you submit them, notes taken in class or handouts etc). Good idea? Yeah. Massive legal hurdles? With the current IP system in the shambles it is, you bet.

fotinorod
about 1 year ago

Hi

I am Lucy, I have found your website while searching for some info at Google. Your site has helped me in a big way.

G’night

Leave your thoughts

Name
Email
  – kept private
Website
  – optional
Human check
  – type in "button"