ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` // public function getWeek() { // Db::name('UserLog')->field('create_time') $data = Db::name('UserLog') // 查询所有数据 ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->group("date") ->order('date', 'asc') // ->whereWeek('create_time') ->select(); $data_first = Db::name('UserLog') // 查询本周数据 ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->group("date") ->order('date', 'asc') ->whereWeek('create_time') ->select(); $monday = mondayTime(time() + 24 * 3600 * 2, false); // 本周星期一 $lastmonday = date("Y-m-d", strtotime("$monday -7 day")); // 上周星期一 // $lastmonday = mondayTime(time(), false); // 上周星期一 // $startTime = strtotime($monday); $startTime = strtotime($lastmonday); // 补全日期 $ArrayData = addArrayData($startTime, $data_first); // 集合点击量 foreach($ArrayData as $k=>$v){ $date[] = explode(',',$v['date']); } foreach($date as $k=>$v){ $date1[] = implode($v); $index = array_search($v,$date); $nums[$index] = $ArrayData[$index]['count']; } $this_week = [ 'date' => $date1, 'count' => $nums ]; $data_second = Db::name('UserLog') // 查询上周数据 ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->group("date") ->order('date', 'asc') ->whereWeek('create_time', 'last week') ->select(); $lastmonday = mondayTime(time(), false); // 上周星期一 $lastStartTime = strtotime($lastmonday); // 补全日期 $lastArrayData = addArrayData($lastStartTime, $data_second); // 集合点击量 foreach($lastArrayData as $k=>$v){ $lastdate[] = explode(',',$v['date']); } foreach($lastdate as $k=>$v){ $lastdate1[] = implode($v); $index = array_search($v,$lastdate); $lastnums[$index] = $lastArrayData[$index]['count']; } $last_week = [ 'date' => $lastdate1, 'count' => $lastnums ]; // ------------------------------------- $param = get_params(); $array = []; if($param){ $day = $param['day']; } else { $day = 7; } for ($i = $day - 1; 0 <= $i; $i--) { $array[] = date('Y-m-d', strtotime('-' . $i . ' day')); $daysnums[] = 0; } $result = DB::table('muapi_user_log') ->field('count(*) as count,date(FROM_UNIXTIME(create_time,"%Y%m%d")) as create_time') ->group("date_format(from_unixtime(create_time),'%Y%m%d')") ->where('DATE_SUB(CURDATE(), INTERVAL '.$day.' DAY) <= date(from_unixtime(create_time))') ->select() ->toArray(); array_walk($result, function ($value, $key) use ($array, &$daysnums) { $index = array_search($value['create_time'],$array); $daysnums[$index] = $value['count']; }); $data = [ 'date' => $array, 'count' => $daysnums ]; return to_assign(0, '', [ // 'data' => $data_first, // 'ArrayData' => $ArrayData, 'this_week' => $this_week, 'last_week' => $last_week, '7days' => $data, ]); $data_three = Db::name('UserLog') // 查询2022-7-4到2019-7-10的数据 ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->group("date") ->order('date', 'asc') ->whereWeek('create_time', '2022-7-4') ->select(); return to_assign(0, '', [ 'data' => $data, // 'date' => $date, // // 'count' => $count, // 'ArrayData' => $ArrayData, // 'initData' => $initData, 'this_week' => $data_first, 'last_week' => $data_second, 'data_three'=>$data_three // 'data_first' => day_document($data_first), // 'data_second' => date_document($data_second), // 'data_three'=>date_document($data_three) ]); } function get_week_xq() { // $date = strtotime(date("Y-m-d")); $date = strtotime("today"); //强制转换日期格式1657296000 $date_str = date('Y-m-d', strtotime("today")); // $date_str = strtotime(date("Y-m-d")); //封装成数组 $arr = explode("-", $date_str); //参数赋值 //年 $year = $arr[0]; //月,输出2位整型,不够2位右对齐 $month = sprintf('%02d', $arr[1]); //日,输出2位整型,不够2位右对齐 $day = sprintf('%02d', $arr[2]); //时分秒默认赋值为0; $hour = $minute = $second = 0; //转换成时间戳 // $strap = int(mktime($hour, $minute, $second, $month, $day, $year)); // $strap = int mktime(int $hour, int $minute, int $second, int $month, int $day, int $year, int ); // $strap = $hour.'-'.$day.'-'.$month.'-'.$year; $strap = date("Y-m-d+H:i:s",time()); // return $strap; //获取数字型星期几 // $number_wk = date("w", $strap); // initData $end_time = strtotime(date("Y-m-d")); $begin_time = strtotime(date("Y-m-d", strtotime("-1 week +1 day", $end_time))); $keys = array_map(function ($time) { return date("Y-m-d", $time); }, range($begin_time, $end_time, 24 * 3600)); // $initData = array_combine($keys, array_fill(0, count($keys), 0)); // $number_wk = date("w");//20220704 $number_wk = date("w", 20220704000000); //获取数字对应的星期 // return $number_wk; //自定义星期数组 //$weekArr = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"); //获取数字对应的星期 //return $weekArr[$number_wk]; return to_assign(0, '', [ 'data' => $number_wk, 'arr' => $year, 'date' => $keys, // // 'count' => $count, // 'ArrayData' => $ArrayData, // // 'initData' => $initData, // 'this_week' => $data_first, // 'last_week' => $data_second, 'weekArr'=>$keys[$number_wk], // // 'data_first' => day_document($data_first), // // 'data_second' => date_document($data_second), // // 'data_three'=>date_document($data_three) ]); } function get_7day() { $order_list = DB::table('muapi_user_log') ->field('count(*) as count,date(FROM_UNIXTIME(create_time,"%Y%m%d")) as create_time') ->group("date_format(from_unixtime(create_time),'%Y%m%d')") ->where('DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(create_time))') ->select(); //sql语 //SELECT count(*) as count,date(FROM_UNIXTIME(create_time,"%Y%m%d")) as create_time,sum(total) as total FROM `qqxz_orders` WHERE ( DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(create_time)) ) GROUP BY date_format(from_unixtime(create_time),'%Y%m%d') $order_list_arr = $order_list->toArray(); if($param){ $day = $param['day']; } else { $day = 7; } for ($i = $day - 1; 0 <= $i; $i--) { $result[] = date('Y-m-d', strtotime('-' . $i . ' day')); $nums[] = 0; // $total[] = 0; } array_walk($order_list_arr, function ($value, $key) use ($result, &$nums) { $index = array_search($value['create_time'],$result); $nums[$index] = $value['count']; // $total[$index] = $value['total']; }); $data = [ 'day' => $result, 'nums' => $nums, // 'total' =>$total ]; halt($data); } // ok 比较复杂 public function getWeek000() { $end_time = strtotime(date("Y-m-d")); $begin_time = strtotime(date("Y-m-d", strtotime("-1 week +1 day", $end_time))); $data_first = DB::table('muapi_user_log') ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->whereTime('create_time', 'between', [$begin_time, $end_time]) ->group("date") ->order('date', 'asc') ->select() ->toArray(); // 补全日期 $ArrayData = addArrayData($begin_time, $data_first); // 集合点击量 foreach($ArrayData as $k=>$v){ $date[] = explode(',',$v['date']); } foreach($date as $k=>$v){ $index = array_search($v,$date); $nums[$index] = $ArrayData[$index]['count']; } return to_assign(0, '', [ 'data' => $data_first, 'ArrayData' => $ArrayData, 'nums' => $nums, ]); } // 7天统计 public function getWeek7() { $end_time = strtotime(date("Y-m-d")); $begin_time = strtotime(date("Y-m-d", strtotime("-1 week +1 day", $end_time))); $array = []; if($param){ $day = $param['day']; } else { $day = 7; } for ($i = $day - 1; 0 <= $i; $i--) { $array[] = date('Y-m-d', strtotime('-' . $i . ' day')); $nums[] = 0; } $result = DB::table('muapi_user_log') ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') // ->whereTime('create_time', 'between', [$begin_time, $end_time]) ->whereTime('create_time', 'between', [$array[0], $array[6]]) ->group("date") ->order('date', 'asc') ->select() ->toArray(); array_walk($result, function ($value, $key) use ($array, &$nums) { $index = array_search($value['date'],$array); $nums[$index] = $value['count']; }); $data = [ 'date' => $array, 'count' => $nums ]; $ArrayData = addArrayData($begin_time, $result); foreach($ArrayData as $k=>$v){ $date[] = explode(',',$v['date']); $index = array_search($v['date'],$date); $nums[$index] = $v['count']; } return to_assign(0, '', [ 'data' => $data, 'ArrayData' => $ArrayData, 'result' => $nums, ]); } // public function getWeek() { // $array = []; // $day = 7; // for ($i = $day - 1; 0 <= $i; $i--) { // $array[] = date('Y-m-d 00:00:00', strtotime('-' . $i . ' day')); // $nums[] = 0; // } // $result = DB::table('data_insert_days') // ->select([ // DB::raw("FROM_UNIXTIME(UNIX_TIMESTAMP(time),'%Y-%m-%d') as date"), // DB::raw('sum(number) AS count'), // ]) // ->whereBetween('time', [Carbon::yesterday()->subDays(7), Carbon::now()]) // ->groupBy("date") // ->orderBy('date', 'asc') // ->get() // ->toArray(); // array_walk($result, function ($value, $key) use ($array, &$nums) { // $index = array_search($value->date,$array); // $nums[$index] = $value->count; // }); // $data = [ // 'date' => $array, // 'count' => $nums // ]; // } function test() { $data_first = Db::name('UserLog') // 查询本周数据 ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->group("date") ->order('date', 'asc') ->whereWeek('create_time') ->select() ->toArray(); // initData 本周 $end_time = strtotime(date("Y-m-d")); $begin_time = strtotime(date("Y-m-d", strtotime("-1 week +1 day", $end_time))); $keys = array_map(function ($time) { return date("Y-m-d", $time); }, range($begin_time, $end_time, 24 * 3600)); // $initData = array_combine($keys, array_fill(0, count($keys), 0)); // 填充0 [0,0,0,0,0,0,0] $count = array_fill(0, count($keys), 0); // array_walk($data_first, function ($value, $key) use ($keys, &$count) { $index = array_search($value['date'],$keys); $count[$index] = $value['count']; }); // "date": ["2022-07-04","2022-07-05","2022-07-06","2022-07-07","2022-07-08","2022-07-09","2022-07-10"], // "count": [3,7,0,0,1,4,0] $data = [ 'all' => array_combine($keys, $count), 'date' => $keys, 'count' => $count ]; // 组合 "2022-07-07": 0, $initData = array_combine($keys, $count); // 上一周 $data_second = Db::name('UserLog') // 查询上周数据 ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->group("date") ->order('date', 'asc') ->whereWeek('create_time', 'last week') ->select() ->toArray(); $last_end_time = strtotime(date("Y-m-d", strtotime("-1 week", $end_time))); $last_begin_time = strtotime(date("Y-m-d", strtotime("-2 week +1 day", $end_time))); $keys2 = array_map(function ($time) { return date("Y-m-d", $time); }, range($last_begin_time, $last_end_time, 24 * 3600)); $count2 = array_fill(0, count($keys2), 0); array_walk($data_second, function ($value, $key) use ($keys2, &$count2) { $index = array_search($value['date'],$keys2); $count2[$index] = $value['count']; }); $data2 = [ 'all' => array_combine($keys2, $count2), 'date' => $keys2, 'count' => $count2 ]; // $initData2 = array_combine($keys2, $count2); // 7days $param = get_params(); if($param){ $day = $param['day']; } else { $day = 7; } $array = []; for ($i = $day - 1; 0 <= $i; $i--) { $array[] = date('Y-m-d', strtotime('-' . $i . ' day')); $daysnums[] = 0; } $result = DB::table('muapi_user_log') ->field('count(*) as count,date(FROM_UNIXTIME(create_time,"%Y%m%d")) as create_time') ->group("date_format(from_unixtime(create_time),'%Y%m%d')") ->where('DATE_SUB(CURDATE(), INTERVAL '.$day.' DAY) <= date(from_unixtime(create_time))') ->select() ->toArray(); array_walk($result, function ($value, $key) use ($array, &$daysnums) { $index = array_search($value['create_time'],$array); $daysnums[$index] = $value['count']; }); $data3 = [ 'date' => $array, 'count' => $daysnums ]; return to_assign(0, '', [ // 'data' => $data, // 'data_first' => $initData, // 'data_second' => $initData2, // 'initData' => $initData, // 'date' => $keys, // 'count' => $count, // 'last_time' => $last_time, // 'last_begin_time' => $last_begin_time, // 'date2' => $keys2, 'this_week' => $data, 'last_week' => $data2, 'data_three'=> $data3, // // 'data_first' => day_document($data_first), // // 'data_second' => date_document($data_second), // // 'data_three'=>date_document($data_three) ]); } // 单独使用的差异 function getMonday() { // $data = mondayTime(time() + 24 * 3600 * 2, false); // 下个星期一 // $data = mondayTime(time(), false); // 本周星期一 // $data = mondayTime(time() - 24 * 3600 * 7, false); // // $begin_time = strtotime(date("Y-m-d", strtotime("-1 week +1 day", $end_time))); $monday = mondayTime(time() + 24 * 3600 * 2, false); // 本周星期一 $lastmonday = date("Y-m-d", strtotime("$monday -7 day")); // 上周星期一 // $end_time = strtotime($monday); // $data = date("Y-m-d", strtotime("-1 week +1 day", $end_time)); return to_assign(0, '', [ // 'data' => $data, 'monday' => $monday, 'lastmonday' => $lastmonday, ]); } public function getWeek00() { $monday = mondayTime(time() + 24 * 3600 * 2, false); // 本周星期一 $lastmonday = date("Y-m-d", strtotime("$monday -7 day")); // 上周星期一 $lastmonday_end_time = strtotime($monday); $lastmonday_begin_time = strtotime(date("Y-m-d", strtotime("$lastmonday"))); //上一周结束 // $lastmonday = date("Y-m-d", strtotime("$monday +7 day")); // 上周星期一 $monday_begin_time = strtotime("$monday"); // 本周星期一 $monday_end_time = strtotime(date("Y-m-d", strtotime("$monday +6 day"))); //上一周结束 // $monday_begin_time = $monday; // $end_time = strtotime(date("Y-m-d")); // $begin_time = strtotime(date("Y-m-d", strtotime("-1 week", $end_time))); // 上周 $data_first = DB::table('muapi_user_log') ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->whereTime('create_time', 'between', [$lastmonday_begin_time, $lastmonday_end_time]) // ->whereWeek('create_time', $monday) ->group("date") ->order('date', 'asc') ->select() ->toArray(); // 本周 $data_second = DB::table('muapi_user_log') ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') ->whereTime('create_time', 'between', [$monday_begin_time, $monday_end_time]) // ->whereWeek('create_time', $monday) ->group("date") ->order('date', 'asc') ->select() ->toArray(); // 补全日期 $ArrayData = addArrayData($lastmonday_begin_time, $data_first); if($data_second) { $mondayArrayData = addArrayData($monday_begin_time, $data_second); // 集合点击量 foreach($mondayArrayData as $k=>$v){ $date1[] = explode(',',$v['date']); } foreach($date1 as $k=>$v){ $index = array_search($v,$date1); $nums1[$index] = $mondayArrayData[$index]['count']; } // $mondayArrayData = 666; } else { $mondayArrayData = array_map(function ($time) { return date("Y-m-d", $time); }, range($monday_begin_time, $monday_end_time, 24 * 3600)); $nums1 = array_fill(0, count($mondayArrayData), 0); } $monday_begin = $monday; // 本周星期一 $monday_end = date("Y-m-d", strtotime("$monday +6 day")); //上一周结束 // initData // $end_time = strtotime(date("Y-m-d")); // $begin_time = strtotime(date("Y-m-d", strtotime("-1 week +1 day", $end_time))); $keys = array_map(function ($time) { return date("Y-m-d", $time); }, range($monday_begin_time, $monday_end_time, 24 * 3600)); $initData = array_combine($keys, array_fill(0, count($keys), 0)); $daysnums = array_fill(0, count($keys), 0); array_walk($data_second, function ($value, $key) use ($keys, &$daysnums) { $index = array_search($value['date'],$keys); $daysnums[$index] = $value['count']; }); $data3 = [ 'date' => $keys, 'count' => $daysnums ]; // 集合点击量 foreach($ArrayData as $k=>$v){ $date[] = explode(',',$v['date']); } foreach($date as $k=>$v){ $index = array_search($v,$date); $nums[$index] = $ArrayData[$index]['count']; } return to_assign(0, '', [ 'data' => $data_first, 'ArrayData' => $ArrayData, 'nums' => $nums, 'mondayArrayData' => $mondayArrayData, 'nums1' => $nums1, 'data_second' => $data_second, 'monday_begin_time' => $monday_begin_time, 'monday_end_time' => $monday_end_time, 'monday_begin' => $monday_begin, 'monday_end' => $monday_end, 'date' => $keys, 'data3' => $data3, 'lastmonday_begin_time' => $lastmonday_begin_time, 'lastmonday_end_time' => $lastmonday_end_time, ]); } public function getW7() { $three_end_time = strtotime('2022-06-28'); $three_time = '2022-06-28'; $three_begin_time = strtotime(date("Y-m-d", strtotime("$three_time +6 day"))); $keys3 = array_map(function ($time) { return date("Y-m-d", $time); }, range($three_begin_time, $three_end_time, 24 * 3600)); $count3 = array_fill(0, count($keys3), 0); return to_assign(0, '', [ // 'data' => $data, // 'data_first' => $initData, // 'data_second' => $initData2, // 'initData' => $initData, // 'date' => $keys, // 'count' => $count, // 'last_time' => $last_time, // 'last_begin_time' => $last_begin_time, // 'date2' => $keys2, // 'this_week' => $data, // 'last_week' => $data2, // 'data_three'=> $data3, 'three_begin_time' => $three_begin_time, 'data_three'=> $keys3, 'count3' => $count3, // // 'data_first' => day_document($data_first), // // 'data_second' => date_document($data_second), // // 'data_three'=>date_document($data_three) ]); } ``` ``` // public function getWeekok() { // // 本周 // $monday = mondayTime(time() + 24 * 3600 * 2, false); // 本周星期一 // $monday_begin_time = strtotime("$monday"); // 本周星期一 // // $lastmonday = date("Y-m-d", strtotime("$monday -7 day")); // 上周星期一 // $monday_end_time = strtotime(date("Y-m-d", strtotime("$monday +6 day"))); //上一周结束 // $data_second = DB::table('muapi_user_log') // ->field('FROM_UNIXTIME(create_time,"%Y-%m-%d") as date,count(create_time) as count') // ->whereTime('create_time', 'between', [$monday_begin_time, $monday_end_time]) // // ->whereWeek('create_time', $monday) // ->group("date") // ->order('date', 'asc') // ->select() // ->toArray(); // $keys = array_map(function ($time) { // return date("Y-m-d", $time); // }, range($monday_begin_time, $monday_end_time, 24 * 3600)); // $daysnums = array_fill(0, count($keys), 0); // array_walk($data_second, function ($value, $key) use ($keys, &$daysnums) { // $index = array_search($value['date'],$keys); // $daysnums[$index] = $value['count']; // }); // $data = [ // 'all' => array_combine($keys, $daysnums), // 'date' => $keys, // 'count' => $daysnums // ]; // return to_assign(0, '', [ // 'data' => $data, // 'date' => $keys, // 'nums' => $daysnums, // ]); // } ``` ``` // 雷达数据 public function getRadar1() { $browser = Db::name('radar') ->field('browser')->select(); // 数组转字符串 foreach ($browser as $k=>$v){ $browserarr[$k] = implode($v); } // return result($browserarr, '获取成功!', 200); // 字符串去重 // $newbrowser=array_unique($browserarr); $newbrowser=array_flip($browserarr); $newbrowser = array_keys($newbrowser); // return result($newbrowser, '获取成功!', 200); // avg平均值 num总分 count统计数量 foreach ($newbrowser as $k=>$v){ $inbrowserarr[$k]['name'] = $v; $inbrowserarr[$k]['value'] = Db::name('radar') ->where('browser',$v) ->sum('num'); } // return result($inbrowserarr, '获取成功!', 200); return to_assign(0, '', ['browser' => $newbrowser, 'num' => $inbrowserarr]); } // --------------------------------------------------------------------- //获取备注信息 public function _get_field() { $model = new \Think\Model(); $controller_name = $this->CamelCaseToUnderScore(CONTROLLER_NAME); $rs = $model->query("select `column_name`,`column_comment` from information_schema.COLUMNS WHERE TABLE_SCHEMA = '".C('DB_NAME')."' AND TABLE_NAME = '".C('DB_PREFIX').$controller_name."'"); return $rs; } //驼峰命名转下划线命名 public function CamelCaseToUnderScore($str){ $dstr = preg_replace_callback('/([A-Z]+)/',function($matchs){ return '_'.strtolower($matchs[0]); },$str); return trim(preg_replace('/_{2,}/','_',$dstr),'_'); } ```