60, 'm3'=>180, 'm5'=>300, 'm15'=>900, 'm30'=>1800, 'h1'=>3600, 'h2'=>7200, 'h4'=>14400, 'h6'=>21600, 'h12'=>43200, 'd1'=>86400, 'd3'=>259200, 'd5'=>432000, 'd7'=>604800, 'd15'=>1296000, ); $redis = Yii::$app->redis; $data = array(); $t = array(); $h = array(); $o = array(); $l = array(); $c = array(); $v = array(); $head = json_decode($redis->lrange($key,0,0)[0],true)['ts']; if($period!='mo'){//除了月级别 其他正常处理 if($from!=-1&&$to!=-1){//起点终点都设置了 if($from<$head){ $startIndex = 0; }else{ $startIndex = floor(($from-$head)/$stset[$period]); } if($to<$head){ $endIndex = 0; }else{ $endIndex = floor(($to-$head)/$stset[$period]); } }else if($from==-1){//只设置了终点 if($to<$head){ $endIndex = 0; }else{ $endIndex = floor(($to-$head)/$stset[$period]); } $startIndex = ($endIndex-$count+1)>0?($endIndex-$count+1):0; }else if($to==-1){//只设置了起点 if($from<$head){ $startIndex = 0; }else{ $startIndex = floor(($from-$head)/$stset[$period]); } $endIndex = ($startIndex+$count-1)>0?:0; }else{ $data['t'] = null; $data['h'] = null; $data['o'] = null; $data['l'] = null; $data['c'] = null; $data['v'] = null; $data['s'] = 'error'; return $data; } }else{//月级别单独处理 $headDate = date('Y-m',$head)."-01"; if($from!=-1&&$to!=-1){//起点终点都设置了 $fromDate = date('Y-m',$from)."-01"; $toDate = date('Y-m',$to)."-01"; if($from<$head){ $startIndex = 0; }else{ $startIndex = $this->getMonthNum($headDate,$fromDate,'-'); } if($to<$head){ $endIndex = 0; }else{ $endIndex = $this->getMonthNum($headDate,$toDate,'-'); } }else if($from==-1){//只设置了终点 $toDate = date('Y-m',$to)."-01"; if($to<$head){ $endIndex = 0; }else{ $endIndex = $this->getMonthNum($headDate,$toDate,'-'); } $startIndex = ($endIndex-$count+1)>0?($endIndex-$count+1):0; }else if($to==-1){//只设置了起点 $fromDate = date('Y-m',$from)."-01"; if($from<$head){ $startIndex = 0; }else{ $startIndex = $this->getMonthNum($headDate,$fromDate,'-'); } $endIndex = ($startIndex+$count-1)>0?($startIndex+$count-1):0; }else{ $data['t'] = null; $data['h'] = null; $data['o'] = null; $data['l'] = null; $data['c'] = null; $data['v'] = null; $data['s'] = 'error'; return $data; } } $result = $redis->lrange($key,$startIndex,$endIndex); foreach ($result as $key=>$val){ $val = json_decode($val,true); $t[] = $val['ts']; $h[] = $val['high']; $o[] = $val['open']; $l[] = $val['low']; $c[] = $val['close']; $v[] = $val['realVol']; } $data['t'] = $t; $data['h'] = $h; $data['o'] = $o; $data['l'] = $l; $data['c'] = $c; $data['v'] = $v; $data['s'] = 'ok'; return $data; } function getMonthNum( $date1, $date2, $tags='-' ){//获取月份差 $date2>$date1 $date1 = explode($tags,$date1); $date2 = explode($tags,$date2); return abs($date1[0] - $date2[0]) * 12 + ($date2[1] - $date1[1]); } }