Skip to content

Instantly share code, notes, and snippets.

@irusri
Last active November 19, 2023 09:27
Show Gist options
  • Select an option

  • Save irusri/e3dddb4b46266008bbfbecfcf84f3919 to your computer and use it in GitHub Desktop.

Select an option

Save irusri/e3dddb4b46266008bbfbecfcf84f3919 to your computer and use it in GitHub Desktop.
RSS feed filter by pubDate
<?php
// GET '$rss_feed_url' using URL param['q']
$rss_feed_url = $_GET['q'];
// Load the RSS feed
$tmpRSS = simplexml_load_file($rss_feed_url);
// Get today's date in the format used by pubDate in the RSS feed
$today_date = date('D, d M Y');
// Process the feed and filter items by today
$Object1 = new StdClass;
for ($x = 0; $x <= count($tmpRSS->channel->item)-2; ) {
$pub_date = date('D, d M Y', strtotime($tmpRSS->channel->item[$x]->pubDate));
if($pub_date===$today_date){
$Object1->items[] = $tmpRSS->channel->item[$x];
}
$x++ ;
}
// Convert JSON to PHP associative array
$data = json_decode(json_encode($Object1), true);
// Create RSS feed
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
// Create the root element for the RSS feed
$rss = $xml->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:content', 'http://purl.org/rss/1.0/modules/content/');
$xml->appendChild($rss);
// Add feed title
$channel = $xml->createElement('channel');
$rss->appendChild($channel);
// Add channel elements (title, link, description, etc.)
$title = $xml->createElement('title', $tmpRSS->channel->title);
$channel->appendChild($title);
$link = $xml->createElement('link', $tmpRSS->channel->link);
$channel->appendChild($link);
$description = $xml->createElement('description', $tmpRSS->channel->description);
$channel->appendChild($description);
// Loop through items
foreach ($data['items'] as $itemData) {
$item = $xml->createElement('item');
$itemTitle = $xml->createElement('title', $itemData['title']);
$itemLink = $xml->createElement('link', $itemData['link']);
$itemDescription = $xml->createElement('description', $itemData['description']);
$itemPubDate = $xml->createElement('pubDate', $itemData['pubDate']);
$item->appendChild($itemTitle);
$item->appendChild($itemLink);
$item->appendChild($itemDescription);
$item->appendChild($itemPubDate);
$channel->appendChild($item);
}
// Save the XML content to a file or output it
header('Content-Type: application/rss+xml; charset=UTF-8');
echo $xml->saveXML(); // Output the XML
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment