Now for a FarAway() function, and we can code up a fairytale!
February 1, 2008 9:37 AM   Subscribe

How do I convert RSS feed timestamps to relative timestamps or "time ago", for example: "12 minutes ago?"

I've got a bunch of aggregated RSS feeds, and I would like to display relative timestamps - "timeago", "timesince" or "fuzzy time", it seems there are many names for it - in the HTML output, like Recent Activity does for registered Mefi users.

Here's an example (self-link). Those of you who have seen Electicker will note that the site already does this (self-link), but those feeds are run through Feeddigest and I'd like to move the legwork to my own server (if only for the more frequent updates).

Do any of you know about an elegant way to do this, preferably in PHP or even Yahoo! Pipes? Note that I will want to do this for a whole bunch of feeds from various sources and native timezones, so I'm guessing there will need to be some timezone conversion (to UTC or something) involved.

Thanks in advance!
posted by goodnewsfortheinsane to Computers & Internet (8 answers total) 2 users marked this as a favorite
The comments on the PHP time man page have functions that do this.
posted by smackfu at 9:44 AM on February 1, 2008

Thanks, smackfu. I had already trawled through that page, but I'm not sure how to implement it in the existing code. For starters, where does it get its timestamp variable from? In the example given above, it gets this feed (Pipe output) to work with. It has timestamps listed thus:

<pubDate>Fri, 01 Feb 2008 08:25:40 PST</pubDate>

Also, internally Pipes also has a "y:published.utime" element which is not included in the output, but I guess may be used for processing at the Pipes stage:


* hour 17
* timezone UTC
* second 27
* month 2
* minute 52
* utime 1201888347
* day 1
* day_of_week 5
* year2008

I just don't get how to feed the RSS timestamp (pubDate or otherwise) into the code, have it convert the timezone, and get it to output the relative timestamp.
posted by goodnewsfortheinsane at 10:06 AM on February 1, 2008

It looks like you have a file called "rss2html.php" which is converting the RSS to HTML. In this file, look for:

$template = FeedForAll_rss2html_str_replace("~~~FeedPubDate~~~", $rss_parser->FeedPubDate, $template);

add the following line after this:

$template = FeedForAll_rss2html_str_replace("~~~FeedRelativeDate~~~", relativeTime($rss_parser->FeedPubDate), $template);

(This assumes relativeTime is a function that takes the RSS time and returns the relative time).



somewhere in your template.
posted by null terminated at 11:29 AM on February 1, 2008

Thanks for the help so far, guys! That seems to work, null terminated: that is, I'm getting output, however:

-I need the pubdate for the item, not the feed;
-I'm using this code, so the function becomes ago(), and I'm not sure how to feed it the ItemPubDate so that it knows to use that for $timestamp.

So when I run it now, I get the difference between now and the start of the epoch. Any ideas?
posted by goodnewsfortheinsane at 1:56 PM on February 1, 2008

Sorry, that code was for the feed date and not the item date.

Below this line:

$item = FeedForAll_rss2html_str_replace("~~~ItemPubLongDate~~~", date($LongDateFormat, $currentItem->pubDate_t), $item);

add the line:

$item = FeedForAll_rss2html_str_replace("~~~ItemRelativeDate~~~", relativeTime($currentItem->pubDate_t), $item);

posted by null terminated at 2:08 PM on February 1, 2008

It works. You can see it in action in the top left pane on Electicker (website link in profile), will add more soon.

I am incredibly chuffed. Thanks so much, null terminated.
posted by goodnewsfortheinsane at 2:21 PM on February 1, 2008

I wrote a little free Javascript library to format timestamps this way. You put in your HTML document snippets like document.write(ago(1126162027)) and the Javascript renders the "6 minutes ago" part client-side. The advantage of doing this in the browser is that the relative timestamps are correct even if the page is loaded out of cache.

You could do this with your PHP solution, but it's probably not worth the trouble.
posted by Nelson at 3:36 PM on February 1, 2008 [1 favorite]

FWIW, I implemented the FeedRelativeDate anyway for a feed that had no per-item pubDate and for which I needed only the single latest item, so that too came in handy. Thanks again!
posted by goodnewsfortheinsane at 3:22 PM on February 15, 2008

« Older What are my obligations to the company with which...   |   How to screen a therapist? Newer »
This thread is closed to new comments.