eyoucms支持多种按时间范围查询数量,支持无限递归查询子栏目

2025-07-20 0 13
if(!function_exists(‘diy_typeids’)) {
    /**
     * 递归获取给定栏目及其所有子栏目的ID列表
     *
     */
    function diy_typeids($typeid)
    {
        $childIds = [$typeid];
        $data = db(‘arctype’)->field(‘id’)->where([
            ‘is_del’ => 0,
            ‘status’ => 1,
            ‘parent_id’ => $typeid
        ])->select();
if ($data instanceof \think\Collection) {
            $data = $data->toArray();
        }
        foreach ($data as $item) {
            $childIds = array_merge($childIds, diy_typeids($item[‘id’]));
        }
        return $childIds;
    }
}
if(!function_exists(‘diy_archive_count’)){
/**
     * 获取发布数量
*/
    function diy_archive_count($range = ‘1’,$typeid = ”)
    {
        $now = time();
        $where = [
‘arcrank’ => 0,
‘is_del’ => 0,
];
        if($typeid){
            $allChildIds = diy_typeids($typeid);
            $where[‘typeid’] = [‘in’, $allChildIds];
        }
//时间范围查询
        if(is_numeric($range)){
$startOfDay = strtotime(date(‘Y-m-d’, $now) . ‘ 00:00:00’);
$endOfDay = strtotime(date(‘Y-m-d’, $now) . ‘ 23:59:59’);
$where[‘add_time’] = [‘between’, [$startOfDay – ($range * 86400), $endOfDay]];
        }else{
            switch ($range) {
                case ‘lastweek’: //上周
$startTime = strtotime(‘last monday’, $now) – 7 * 86400;
$endTime = strtotime(‘last sunday’, $now) + 86399;
break;
                case ‘thisweek’: //本周
$startTime = strtotime(‘last sunday’, $now) + 86400;
$endTime = strtotime(‘next sunday’, $now) – 1;
                    break;
                case ‘thismonth’: //本月
$startTime = mktime(0, 0, 0, date(‘n’, $now), 1, date(‘Y’, $now));
$endTime = mktime(23, 59, 59, date(‘n’, $now), date(‘t’, $now), date(‘Y’, $now));
                    break;
                case ‘lastmonth’: //上个月
$startTime = mktime(0, 0, 0, date(‘n’, $now)-1, 1, date(‘Y’, $now));
$endTime = mktime(23, 59, 59, date(‘n’, $now)-1, date(‘t’, mktime(0, 0, 0, date(‘n’, $now)-1, 1, date(‘Y’, $now))), date(‘Y’, $now));
                    break;
case ‘thisyear’: //今年
$startTime = mktime(0, 0, 0, 1, 1, date(‘Y’, $now));
$endTime = mktime(23, 59, 59, 12, 31, date(‘Y’, $now));
break;
case ‘lastyear’: //去年
$startTime = mktime(0, 0, 0, 1, 1, date(‘Y’, $now) – 1);
$endTime = mktime(23, 59, 59, 12, 31, date(‘Y’, $now) – 1);
break;
                default:
                    return false;
            }
$where[‘add_time’] = [‘between’, [$startTime, $endTime]];
        }
        return db(‘archives’)->where($where)->count();
    }
}
以上代码复制到根目录/extend/function.php 最下方
然后再模版里调用
{:diy_archive_count()} 默认当天数量
{:diy_archive_count(7)}  近7天的数量
{:diy_archive_count(30)} 近30天的数量
{:diy_archive_count(365)} 近365天的数量
其他的数字以此类推
其他查询方式
{:diy_archive_count(lastweek)} = 查询上周数量
{:diy_archive_count(thisweek)} = 查询本周数量
{:diy_archive_count(thismonth)} = 查询本月数量
{:diy_archive_count(lastmonth)} = 查询上月数量
{:diy_archive_count(thisyear)} = 查询今年数量
{:diy_archive_count(lastyear)} = 查询去年数量
查询某个栏目以及下属的所有子栏目
{:diy_archive_count(时间,栏目ID)}
比如
{:diy_archive_count(30,1)} 相当于查询最近30天栏目1的数量,第一个参数适用以上的
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

版权声明:所有的源码、软件和资料,不得使用于非法商业用途,不得违反国家法律,一切关于该资源商业行为与本站无关。

免费cms模板 eyoucms教程 eyoucms支持多种按时间范围查询数量,支持无限递归查询子栏目 https://www.mianfeicms.com/331.html

相关文章

发表评论
暂无评论