关于时间的处理(时间范围生成,mysql按照天,周,月统计)
mysql的查询语句
#周 select FROM_UNIXTIME(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks; #天 select FROM_UNIXTIME(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days; #月 select FROM_UNIXTIME(create_time,'%Y%m') months,count(caseid) count from tc_case group by months;
PHP时间范围生成
/**
* @Author tony (*****************)
* @param $startTimestamp
* @param int $num
* @param string $format
* @param int $step
* @param string $stepType
* @return array
*/
function dateRange($startTimestamp, $num = 10, $format = '', $step = 1, $stepType = 'days')
{
$dateRange = array();
$tempTimeStamp = $startTimestamp;
for($i = 0; $i < $num; $i++) {
$tempTimeStamp = strtotime('+' . $step . ' ' . $stepType, $tempTimeStamp);
if(empty($format)) {
$dateRange[] = $tempTimeStamp;
} else {
$dateRange[] = date($format, $tempTimeStamp);
}
}
return $dateRange;
}
var_dump(dateRange(time(), 15, 'Y-m-d', -1, 'months'));