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

如何按日月过滤XML实现Joomla模块"历史上的今天"功能

需求背景

我正在尝试实现XML文件过滤功能,仅展示日期与当前月、日一致的条目,也就是类似“历史上的今天”的效果:比如今日为10月14日,就展示所有XML中日期为10月14日的内容,忽略年份限制。这段代码属于我正在自定义的Joomla模块的一部分,我希望通过改造它来展示自定义历史事件。

XML文件示例

<?xml version="1.0" encoding="utf-8"?>
<articles>
    <article>
        <url>/someurl</url>
        <title>Title</title>
        <text><![CDATA[Event info text]]></text>
        <date>1914-10-14</date>
        <date_publish>0</date_publish>
        <image>/image.jpg</image>
    </article>
    <article>
        <url>/someurl</url>
        <title>Title</title>
        <text><![CDATA[Event info text]]></text>
        <date>1945-10-14</date>
        <date_publish>0</date_publish>
        <image>/image.jpg</image>
    </article>
</articles>

现有PHP代码

class NSP_GK5_xml_file_Model {
    // Method to get sources of articles
    static function getSources($config) {
        $content = array();
        // if there are selected files - set the variables
        if($config['xml_file'] != -1 && file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
            // loading file content
            $file_content = file_get_contents(JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk5' . DS . 'external_data' . DS . $config['xml_file']);
            //
            $xml = new SimpleXMLElement($file_content);
            //
            if(count($xml->article) > 0) {
                //
                $art = array();
                //
                foreach ($xml->article as $element) {
                    //
                    foreach($element as $key => $value) {
                        $art[$key] = (string) $value;
                    }
                    //
                    array_push($content, (array) $art);
                }
            }
        }
        //
        return $content;
    }
    // Method to get articles in standard mode 
    static function getArticles($items, $config, $amount) { 
        $content = array();
        //
        for($i = $config['offset']; $i < $amount + $config['offset']; $i++) {
            if(isset($items[$i])) {
                array_push($content, $items[$i]);
            }
        }
        // the content array
        return $content; 
    }
}

// EOF
改造方案

直接在getSources方法读取XML节点的逻辑里增加月日匹配过滤即可:首先获取当前系统时间的m-d格式月日字符串,遍历每个article节点时,提取date字段的后5位(对应月日部分)和当前月日对比,匹配成功才加入返回数组。

改造后的完整代码

class NSP_GK5_xml_file_Model {
    // Method to get sources of articles
    static function getSources($config) {
        $content = array();
        // 获取当前月日 格式为m-d 示例值:10-14
        $current_month_day = date('m-d');
        // if there are selected files - set the variables
        if($config['xml_file'] != -1 && file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
            // loading file content
            $file_content = file_get_contents(JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk5' . DS . 'external_data' . DS . $config['xml_file']);
            //
            $xml = new SimpleXMLElement($file_content);
            //
            if(count($xml->article) > 0) {
                //
                $art = array();
                //
                foreach ($xml->article as $element) {
                    //
                    foreach($element as $key => $value) {
                        $art[$key] = (string) $value;
                    }
                    // 增加月日匹配校验 仅匹配成功的条目加入结果
                    $event_month_day = substr($art['date'], 5);
                    if($event_month_day == $current_month_day) {
                        array_push($content, (array) $art);
                    }
                }
            }
        }
        //
        return $content;
    }
    // Method to get articles in standard mode 
    static function getArticles($items, $config, $amount) { 
        $content = array();
        //
        for($i = $config['offset']; $i < $amount + $config['offset']; $i++) {
            if(isset($items[$i])) {
                array_push($content, $items[$i]);
            }
        }
        // the content array
        return $content; 
    }
}

// EOF

注意事项

  • 确保XML中所有date字段的格式统一为Y-m-d,如果存在其他格式需要对应调整截取规则
  • 如果需要适配国内时区,可以在获取日期前增加时区设置代码:date_default_timezone_set('Asia/Shanghai')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 08:36:04