简体   繁体   中英

How can I get data from RSS by date in PHP?

I am working on a small script where I wanna take only my website's latest post which is posted yesterday mean I wanna get all yesterday's links and titles. I tried with my script but I am getting all URLs I am not sure how can I fix it.

Can anyone help me solve this problem?

I was wondering if I can use 'where' attribute like we usually use in SQL. I want only 1 days posts to be scraped.

<?php
header('Content-Type: application/json');
$url = "https://www.lifegoals.co.in/feed/";
$i=0;
 $invalidurl = false;
 if(@simplexml_load_file($url)){
  $feeds = simplexml_load_file($url);
 }else{
  $invalidurl = true;
  echo "<h2>Invalid RSS feed URL.</h2>";
 }
 if(!empty($feeds)){

  //$site = $feeds->channel->title;
  //$sitelink = $feeds->channel->link;

  //echo "<h1>".$site."</h1>";
  foreach ($feeds->channel->item as $item) {

   $title = $item->title;
   $link = $item->link;
   //$description = $item->description;
   $postDate = $item->pubDate;
   $pubDate = date('D, d M Y',strtotime($postDate));
   $currDate = date('D, d M Y');
   if($i>=10) break;   
   if($pubDate=$currDate){
   $rss =   "<item>
                <title>$title</title>
                <link>$link</link>
            </item>";     
            
   echo $rss;   
   $i++;
   }
  } 
  }
?>

i want only 1 days posts there are 4 days posts

I'd add some debugging to this to ensure that you're getting what you think you want. Try the following in your foreach loop:


    print_r([
        $postDate,
        $pubDate,
        $currDate,
        ($pubDate == $currDate),
    ]);
   if($pubDate==$currDate){
   $rss =   "<item>
                <title>$title</title>
                <link>$link</link>
            </item>";     
            
   echo $rss;   
   $i++;
   }

The == was missing thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM