You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP RSS脚本需求:过滤今日内容并按时间降序展示

如何过滤RSS条目只显示今日内容

嘿,我看了你的PHP RSS脚本,要只展示今日的条目其实很简单,咱们在排序之后加个过滤步骤就行。下面是修改后的完整代码,关键改动我都加了注释:

<?php 
//Feed URLs
$feeds = array(
"https://www.abda.de/?type=1102",
"https://www.abda.de/?type=1100",
);
//Read each feed's items
$entries = array();
foreach($feeds as $feed) {
    $xml = simplexml_load_file($feed);
    $entries = array_merge($entries, $xml->xpath("//item"));
}
//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
    return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});

// --- 新增:过滤出今日发布的条目 ---
$todayStart = strtotime('today'); // 今日00:00的时间戳
$todayEnd = strtotime('tomorrow'); // 明日00:00的时间戳(即今日23:59:59的下一秒)
$entries = array_filter($entries, function($entry) use ($todayStart, $todayEnd) {
    $entryPubTime = strtotime($entry->pubDate);
    // 判断条目发布时间是否在今日范围内
    return $entryPubTime >= $todayStart && $entryPubTime < $todayEnd;
});

function showrssitem($i) {
    global $entries;
    $backgroundbutton = 'd3d3d3';
    switch ($entries[$i]->category) {
        case "Chargenrückrufe" : $backgroundbutton = 'ff9933'; /* orange */ break;
        case "Rückrufe" : $backgroundbutton = 'ff9933'; /* orange */ break;
        case "Informationen der Institutionen und Behörden" : $backgroundbutton = 'ff3300'; /* red */ break;
        case "Informationen der Hersteller" : $backgroundbutton = 'ff5500'; /* red 2 */ break;
        case "AMK-PhaGro-Schnellinformationen" : $backgroundbutton = 'ff7700'; /* red3 */ break;
    }
    $strippedentry1 = str_replace ("Online-Nachricht: ","", $entries[$i]->title);
    $strippedentry2 = str_replace ("Informationen der Institutionen und Behörden: ","", $strippedentry1);
    $strippedentry3 = str_replace ("Informationen der Hersteller: ","", $strippedentry2);
    echo '<a style="text-decoration: none;" target="_blank" href="' . $entries[$i]->link . '"><button style="background-color: #' . $backgroundbutton . '; margin-bottom: 10px; width: 100%; height:75px; display: block; border-radius: 10px;">' . $entries[$i]->category . '</button></a>';
    echo mb_strimwidth($strippedentry3,0,100,"...") . "<br>";
    echo "<i>[" . strftime('%d/%m/%Y', strtotime($entries[$i]->pubDate)) . "]</i><br>";
}

// --- 新增:如果今日没有条目,显示提示 ---
if (empty($entries)) {
    echo "<p>今日暂无相关RSS条目</p>";
}
?>

关键改动说明:

  • 过滤逻辑:用array_filter()遍历排序后的条目,通过时间戳对比,只保留发布时间在今日00:00到明日00:00之间的内容,这样能精准匹配今日所有时段的条目
  • 变量作用域:用use ($todayStart, $todayEnd)把外部的时间戳变量传入匿名函数,确保能正确读取
  • 空状态处理:加了empty($entries)的判断,如果今日没有符合条件的条目,会显示提示文本,避免页面空白

内容的提问来源于stack exchange,提问作者Riekelt Keuter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:37:26