周、月开始结束日期计算——排行榜

 1      // 获取指定日期所在月的开始日期与结束日期
 2      function getMonth($date){
 3          $ret=array();
 4          $timestamp=strtotime($date);
 5          $mdays=date('t',$timestamp);
 6          $ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
 7          $ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
 8          return $ret;
 9      }
10      
11      // 获取指定日期所在星期的开始时间与结束时间
12      function getWeek($date){
13          $ret=array();
14          $timestamp=strtotime($date);
15          $w=strftime('%u',$timestamp);
16          $ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
17          $ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
18          return $ret;
19      }
20 
21     //周排行榜
22     public function weekRanking(){
23       $ret=array();
24       //本周开始和结束日期数据
25       $ret=$this->getWeek(date('Y-m-d'));
26     //以下进行自己的逻辑操作
27       $newUploadWeek = (new BooksModel)->timeSearch($ret['sdate'],$ret['edate'],"*");
28     //统计相关数据
29       $uplist=array();
30       $num = array();
31       foreach ($newUploadWeek as $key => $up) {
32           if( in_array($up['uid'],$uplist) ){
33               $num[$up['uid']]=$num[$up['uid']]+1;
34           }else{
35               $uplist[] = $up['uid'];
36               $num[$up['uid']]= 1;
37           }
38       }
39     //根据数量倒序排行(从大到小)
40       arsort($num);
41       $ranking = $num;
42     //取排行榜前十名
43       $ranking = array_slice($num,0,10,true);//true键值保留,false键值重置print_r($rankName);
44     }
45 
46     //上传月排行榜
47     public function uploadMouthRanking(){
48       $ret=array();
49       //本月开始与结束日期
50       $mouth=$this->getMonth(date('Y-m-d'));
51       /*
52       进行逻辑操作
53       */
54       $ranking="月排行榜";
55       print_r($ranking);
56     }
57 
58     //获取其他一些时间周期
59     function getCycleTime($cycleTime){
60          $ret=array();
61          switch($cycleTime){
62              case yesterday:
63                 // 昨天
64                 $ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
65                 $ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
66                 break;
67              case thisWeek:
68                 //本星期
69                 $ret=getWeekRange(date('Y-m-d'));
70                 break;
71              case LastWeek:
72                 //上一个星期
73                 $strDate=date('Y-m-d',strtotime('-1 week'));
74                 $ret=getWeekRange($strDate);
75                 break;
76              case theWeekBeforeLast: 
77                 //上上星期
78                 $strDate=date('Y-m-d',strtotime('-2 week'));
79                 $ret=getWeekRange($strDate);
80                 break;
81              case thisMouth: 
82                 //本月
83                 $ret=getMonthRange(date('Y-m-d'));
84                 break;
85              case lastMouth:
86                 //上月
87                 $strDate=date('Y-m-d',strtotime('-1 month'));
88                 $ret=getMonthRange($strDate);
89              break;
90          }
91          return $ret;
92      }

 

posted @ 2017-12-28 16:44  大妖小白  阅读(345)  评论(0编辑  收藏  举报