數據庫字段為 varchar 長度足夠,以時間戳的形式加入
然后對這些時間按月份做一個統計分類計數處理
SQL:SELECT FROM_UNIXTIME(popu_time,'%Y-%m') months,COUNT(popu_no) COUNT FROM sm_popu GROUP BY months ORDER BY months DESC
直接全局搜索存入變量中
$months= Yii::app()->db->createCommand($sql)->queryAll();
把數組以select展示在前台,並且改變的時候直接以GET方式帶參數跳轉頁面
<select onchange="window.location=options[(this.selectedIndex)].value" class="location_select" style="width:175px;"> <option>선택하다.</option> <? foreach($months as $_k=>$_v){?> <option value="<?= $this->createUrl('popu',array('o'=>$_k))?>"><?= $_v['months'].'('.$_v['COUNT'].')'?></option> <? } ?>
</select>
並且對參數進行尋找月份,並且掐定開始與結束位置
1 if($o==0){ 2 //$stopD=date('Y-m-d',time()+86400); 3 $stopD=date('Y-m-d', strtotime($months[$o]['months'].'-01')+(86400*31)); 4 $startD=$months[$o]['months'].'-01'; 5 }elseif($o==sizeof($months)){ 6 $startD=$months[$o]['months'].'-01'; 7 $stopD='2010-01-01'; 8 }else{ 9 $startD=$months[$o]['months'].'-01'; 10 $stopD=$months[$o-1]['months'].'-01'; 11 }
最后根據前后時間加入篩選條件
$cOb = new CDbCriteria(); $cOb -> addCondition("popu_time>=UNIX_TIMESTAMP('$startD') and popu_time<UNIX_TIMESTAMP('$stopD')");
最終列表為2個時間段之間的數據列表