摘要:
... 想了好久,最后找到 strtotime函数,下面分享一下实现的方法,大家以后也不用那么麻烦了。
$days = array (); #首先定义一个空数组
for($i=0;$i<=30;$i++)
{
$days[] = date(“Y-m-d”, strtotime(‘ -‘. $i . ‘day’));
}
通过循环把前30天的具体日期存放到数组$days中,然后就可以通过 foreach()函数输入具体日期了,示例:
foreach($days as $day)
{
echo $day;
}
这样就可以成功获取前30天的具体日期,当前你也可以只获取10天或1星期。
获取指定日期内每天的开始和结束时间
/**
*获取指定一段时间内的每天的开始时间
* @param $startdate 开始日期 时间戳
* @param $enddate 结束日期 时间戳
* @param $format 时间格式 0:时间戳 1日期格式
* @return array 返回一维数组 */
public static function PeriodTime($stimestamp, $etimestamp, $format=0) { $days = floor(($etimestamp-$stimestamp)/86400+1); // 保存每天日期
$date = array(); if ($days == 1) { //一天内
$date[0]['start'] = $stimestamp; $date[0]['end'] = $etimestamp;
}else{ for($i=0; $i<$days; $i++){ if ($format==0) { $date[$i]['start'] = $stimestamp+(86400*$i); $date[$i]['end'] = strtotime(date('Y-m-d 23:59:59', $date[$i]['start']));
}else{ $date[$i]['start'] = date('Y-m-d 00:00:00', $stimestamp+(86400*$i)); $date[$i]['end'] = date('Y-m-d 23:59:59', $stimestamp+(86400*$i));
}
}
} //结果
return $date;
}

