thinkphp5统计本月每天注册的人数代码片段记录(注册时间为时间戳)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/p_function/article/details/79389349
public function countMonthUsers()
    {
        $month = date('Y-m',time());
        $prefix = config('database.prefix');
        $semRes = Db::query("select FROM_UNIXTIME(create_time,'%Y-%m-%d') days,count(id) count from ".$prefix."user_submit WHERE FROM_UNIXTIME(create_time,'%Y-%m') = '".$month."' AND delete_time = 0 group by days");

        $seoRes = Db::query("select FROM_UNIXTIME(create_time,'%Y-%m-%d') days,count(id) count from ".$prefix."user_submit WHERE FROM_UNIXTIME(create_time,'%Y-%m') = '".$month."' AND delete_time = 0 group by days");


        // x 轴数据,作为 x 轴标注
        $j = date("t"); //获取当前月份天数
        $start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
        $xData = array();
        for($i=0;$i<$j;$i++)
        {
            $xData[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
        }
        //处理获取到的数据
        $ySemData = array();
        if(!empty($semRes))
        {
            foreach ($xData as $k=>$v)
            {
                foreach ($semRes as $kk=>$vv)
                {
                    if($v == $vv['days'])
                    {
                        $ySemData[$k] = $vv['count'];
                        break;
                    }else{
                        $ySemData[$k] = 0;
                        continue;
                    }
                }
            }
        }else{
            foreach ($xData as $k=>$v)
            {
                $ySemData[$k] = 0;
            }
        }

        $ySeoData = array();
        if(!empty($seoRes))
        {
            foreach ($xData as $k=>$v)
            {
                foreach ($seoRes as $kk=>$vv)
                {
                    if($v == $vv['days'])
                    {
                        $ySeoData[$k] = $vv['count'];
                        break;
                    }else{
                        $ySeoData[$k] = 0;
                        continue;
                    }
                }
            }
        }else{
            foreach ($xData as $k=>$v)
            {
                $ySeoData[$k] = 0;
            }
        }
        return [$xData,$ySemData,$ySeoData];
    }

上面代码用的thinkphp5框架,最后获取到的数据时下面的样子

array(3) {
  [0] => array(28) {
    [0] => string(10) "2018-02-01"
    [1] => string(10) "2018-02-02"
    [2] => string(10) "2018-02-03"
    [3] => string(10) "2018-02-04"
    [4] => string(10) "2018-02-05"
    [5] => string(10) "2018-02-06"
    [6] => string(10) "2018-02-07"
    [7] => string(10) "2018-02-08"
    [8] => string(10) "2018-02-09"
    [9] => string(10) "2018-02-10"
    [10] => string(10) "2018-02-11"
    [11] => string(10) "2018-02-12"
    [12] => string(10) "2018-02-13"
    [13] => string(10) "2018-02-14"
    [14] => string(10) "2018-02-15"
    [15] => string(10) "2018-02-16"
    [16] => string(10) "2018-02-17"
    [17] => string(10) "2018-02-18"
    [18] => string(10) "2018-02-19"
    [19] => string(10) "2018-02-20"
    [20] => string(10) "2018-02-21"
    [21] => string(10) "2018-02-22"
    [22] => string(10) "2018-02-23"
    [23] => string(10) "2018-02-24"
    [24] => string(10) "2018-02-25"
    [25] => string(10) "2018-02-26"
    [26] => string(10) "2018-02-27"
    [27] => string(10) "2018-02-28"
  }
  [1] => array(28) {
    [0] => int(0)
    [1] => int(0)
    [2] => int(0)
    [3] => int(0)
    [4] => int(0)
    [5] => int(0)
    [6] => int(0)
    [7] => int(0)
    [8] => int(0)
    [9] => int(0)
    [10] => int(0)
    [11] => int(0)
    [12] => int(0)
    [13] => int(0)
    [14] => int(0)
    [15] => int(0)
    [16] => int(0)
    [17] => int(0)
    [18] => int(0)
    [19] => int(0)
    [20] => int(0)
    [21] => int(0)
    [22] => int(0)
    [23] => int(30)
    [24] => int(0)
    [25] => int(6)
    [26] => int(17)
    [27] => int(0)
  }
  [2] => array(28) {
    [0] => int(0)
    [1] => int(0)
    [2] => int(0)
    [3] => int(0)
    [4] => int(0)
    [5] => int(0)
    [6] => int(0)
    [7] => int(0)
    [8] => int(0)
    [9] => int(0)
    [10] => int(0)
    [11] => int(0)
    [12] => int(0)
    [13] => int(0)
    [14] => int(0)
    [15] => int(0)
    [16] => int(0)
    [17] => int(0)
    [18] => int(0)
    [19] => int(0)
    [20] => int(0)
    [21] => int(0)
    [22] => int(0)
    [23] => int(0)
    [24] => int(0)
    [25] => int(0)
    [26] => int(6)
    [27] => int(0)
  }
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页