[php]代码库
//本功能纯属当前项目需要所写,有更好的方法欢迎大家留言探讨
//计算日期所在月份的起始和结束时间
function getMonthRange($date){
$ret=array();
$timestamp=strtotime($date);
$mdays=date('t',$timestamp);
$ret['sdate']=strtotime(date('Y-m-1 00:00:00',$timestamp));
$ret['edate']=strtotime(date('Y-m-'.$mdays.' 23:59:59',$timestamp));
return $ret;
}
//时间分段显示
function getTimePartRange($year,$month,$part){ //年、月,分组个数
$nyear = date('Y',time());
$nmonth = date('m',time());
$nday = date('d',time());
$retday = getMonthRange($year.'-'.$month.'-'.$nday);
$sdate = date('d',$retday['sdate']);
$edate = date('d',$retday['edate']);
if($year==$nyear && $month==$nmonth){
$edate=$nday;
}
for($i=1;$i<=$edate;$i++){
$timeArray[] = $i;
}
$countAry = count($timeArray);
$intAry = floor($countAry/$part); //舍去法取整,为0的时候,表示分组个数大于了日期数,多出的分组个数值为0
$modAry = $countAry%$part; //取余数
$modDay = $part-$modAry; //计算多余天数的分配起始位置
//将时间切割出时段
$z = 0;
if($intAry!=0){
$d = $intAry;
}else{
$d = 1;
}
for($i=1;$i<=$part;$i++){
$timePart[$i] = array_slice($timeArray,$z,$d);
$z += $d;
if($intAry!=0){
if($i == $modDay){
$d = $d+1;
}
}
}
return $timePart;
}
//调用实例
$timePart = getTimePartRange(2015,4,7); //年,月,分组个数
foreach($timePart as $k => $v){
$datePart[$k]['sdate'] = $year.'-'.$month.'-'.$v[0].' 00:00:00';
$datePart[$k]['edate'] = $year.'-'.$month.'-'.end($v).' 23:59:59';
}