版权声明:本人原创文章,转载时请保留所有权并以超链接形式标明文章出处 https://blog.csdn.net/qq_37138818/article/details/82855010
在项目的开发中 可能会遇到 这样的需求
比如数据库的字段 start_time end_time
开始时间 是 20180909
结束时间 是 20180913
那么对应的 天数是4天的 那么就要将该数据 循环4次
如下是select 查询出来的 数据
public function test1()
{
header("Access-Control-Allow-Origin: *");
$y = date("Y", time());
$m = date("m", time());
$d = date("d", time());
$t0 = date('t'); // 本月一共有几天
$start_month = mktime(0, 0, 0, $m, 1, $y); // 创建本月开始时间
$end_month = mktime(23, 59, 59, $m, $t0, $y); // 创建本月结束时间
$model = new AdvertModel();
$data = $model->type_time($start_month, $end_month, $adsense_id);//这里面有多条数据
$ar = [];
$data=$this->coun($data,$ar);
if ($data) {
exit(json_encode(array("status" => 1, "advert" => $data)));
} else {
exit(json_encode(array("status" => 0, "msg" => "当前没有数据")));
}
}
/*
* 公共方法
* 参数 data: 多条数据
* ar 声明的数组
* end_time 结束时间
* start_time 开始时间
* data_index 新的健名
*/
public function coun($data,$ar,$end_time="end_time",$start_time="start_time",$date_index="date_index"){
foreach ($data as $key =>$i) {
$count_days=(($i[$end_time]- $i[$start_time])/60/60/24);
if ($count_days>0){
for ($j=0;$j<$count_days;$j++){
$date_index = date("Ymd",$i[$start_time]+ 3600*24*$j);
$i[$date_index] = $date_index ;
$ar[$date_index] = $i;
}
}
}
return $ar;
}
如整合期间有遇到什么问题 可以加群 858507220 一起讨论哦。