select("*")->from($sqlName) ->where('cmd')->eq(6901) ->andWhere('zoneid')->eq($zoneid) ->andWhere('uid')->eq($uid) ->fetchAll(); $lastItem = null; $dic = new stdClass(); $list = array(); $index = 1; foreach ($arr as $item) { if($lastItem != null){ $list[] = $lastItem; if(strtotime($item->ts) - strtotime($lastItem->ts) > 20*60){ $timeDic = new stdClass(); $timeDic->startTs = strtotime($list[0]->ts); $timeDic->endTs = strtotime($list[count($list)-1]->ts); $timeDic->durationTs = strtotime($list[count($list)-1]->ts) - strtotime($list[0]->ts); $dic->$index = $timeDic; $index += 1; $list = array(); } } $lastItem = $item; } $result = new stdClass(); $result->data = $dic; echo json_encode($result);