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')){
/**
* 獲取發(fā)布數(shù)量
*/
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();
}
}
以上代碼復(fù)制到根目錄/extend/function.php 最下方
然后再模版里調(diào)用
{:diy_archive_count()} 默認(rèn)當(dāng)天數(shù)量
{:diy_archive_count(7)} 近7天的數(shù)量
{:diy_archive_count(30)} 近30天的數(shù)量
{:diy_archive_count(365)} 近365天的數(shù)量
其他的數(shù)字以此類推
其他查詢方式
{:diy_archive_count(lastweek)} = 查詢上周數(shù)量
{:diy_archive_count(thisweek)} = 查詢本周數(shù)量
{:diy_archive_count(thismonth)} = 查詢本月數(shù)量
{:diy_archive_count(lastmonth)} = 查詢上月數(shù)量
{:diy_archive_count(thisyear)} = 查詢今年數(shù)量
{:diy_archive_count(lastyear)} = 查詢?nèi)ツ陻?shù)量
查詢某個欄目以及下屬的所有子欄目
{:diy_archive_count(時間,欄目ID)}
比如
{:diy_archive_count(30,1)} 相當(dāng)于查詢最近30天欄目1的數(shù)量,第一個參數(shù)適用以上的