Fix a bad feed date using SimplePie
June 22 2008 04:41 am | SimplePie
Update: Added method that works in SimplePie Wordpress Plugin.
If you have a bad rss feed or atom feed date, here is a way to use SimplePie to fix it:
<?php
require_once 'simplepie.inc';
class FixDates extends SimplePie_Item
{
function FixDates($feed, $data)
{
$this->feed = $feed;
$this->data = $data;
$this->fix_date();
}
function fix_date()
{
$parsed = $this->get_date('U');
//
// put code to fix date here
//
// example: add one hour:
$newParsed = $parsed + 3600;
$this->data['date']['parsed'] = $newParsed;
$newRaw = gmdate("D, d M Y H:i:s",$newParsed);
$this->data['date']['raw'] = $newRaw;
}
}
$badDatesFeedUrl = 'http://feeds.feedburner.com/Mashable';
$goodDatesFeedUrls = array(
'http://feeds.engadget.com/weblogsinc/engadget',
'http://feeds.boingboing.net/boingboing/iBag',
'http://feeds.feedburner.com/TechCrunch'
);
$fixedDates = new SimplePie();
$fixedDates->set_feed_url($badDatesFeedUrl);
$fixedDates->set_item_class('FixDates');
$fixedDates->init();
$goodDates = new SimplePie();
$goodDates->set_feed_url($goodDatesFeedUrls);
$goodDates->init();
$merged = SimplePie::merge_items(array($fixedDates,$goodDates));
foreach($merged as $item)
{
echo $item->get_link().'<br>';
echo '<small>'.$item->get_date().'</small><br>';
echo '<br><br>';
}
?>
SimplePie Wordpress Plugin
The following code is the method for fixing a bad date in a feed for the SimplePie Wordpress Plugin. This method only works with single feeds, not multifeeds. The reason for that is because there is no way to pre-process feeds in SPWP, only post-process. Therefore if you use this method for multifeeds, the items won’t display in the correct sort order.
In your call to SimplePieWP, add these options to your options array:
‘processing’=>’fix_date’
‘date_format’=>’U’
The ‘processing option’ tells SPWP to call the ‘fix_date’ process that will fix the date.
The ‘date_format’ option tells SPWP to format the date into the format fix_date needs.
<?php
echo SimplePieWP('http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss',
array(
'processing'=>'fix_date',
'date_format'=>'U'
)
);
?>
Create a file named fix_date.php, insert the PHP code below, and upload that file to the processing directory which resides in the SPWP plugin directory.
<?php
/* For: SimplePie Plugin for Wordpress
Function: Fix wrong date
License: BSD (http://www.opensource.org/licenses/bsd-license.php)
Author: Michael Shipley
Help: http://tech.groups.yahoo.com/group/simplepie-support/
*/
class SimplePie_PostProcess
{
function item_date($date)
{
// fix date here. example adds 1 hour
$date += 3600; // add one hour (3600 seconds)
return date('l, j F Y, g:i a',$date);
}
}
?>



marin on 23 Jun 2008 at 5:59 pm #
Thanks for this article!
I have a question. I use Simplepie plugin for WP and my rss time is not correct. Where should i put your code to fix it?
Thanks, Marin
Michael Shipley on 23 Jun 2008 at 9:33 pm #
Hi Marin,
It should be possible, using SPWP’s post processing feature, to alter the date. It would cause a problem though with merged feeds. If only one feed’s date is altered, the items won’t seem to be sorted correctly since the feeds are sorted before the date is altered. But for a single feed, that wouldn’t be a problem. Is this a single feed situation or multifeed? What feed has the bad date?
Either way, I’ll work on the SPWP method and post an update to this article asap.
marin on 24 Jun 2008 at 2:23 am #
Hey, thanks for the reply.
. I had had this problems before, with displaying latest forum posts (vbulletin rss) with other metods (not Simplepie). I still don’t understand the core of this problem. Isn’t there a published date assigned to each rss? Does this change in process or it just doesn’t care?
It’s a multifeed
marin on 26 Jun 2008 at 12:31 pm #
Hey Michael!
Thanks for your time, it works great. I use it to display my latest forum posts.
Thanks for you time!
But it won’t work with multifeed ha? Pity
Thanks, Marin
henri on 17 Jul 2008 at 12:13 pm #
Hi Michael,
I want to rewritte the url display with SPWP and I know that I can do it with post processing but I have no idea how to do that.
The aim is to rewritte url like that :
http://www.domaine2.com/url to http://www.domaine1.com/goto/domaine2.com/url
So that I can track the number of clics back!
Thanks for your kind help
henri