我正在开发一个项目,该项目需要安排能够在特定时间内重复这些事件的事件。我的目标是从表单中传递初始日期并将其递增7,以便日期将在同一工作日落入所选的迭代次数。我已经考虑了延续到新月和新年的日期。然而,在循环结束时,日期结束。错误的一个例子:
错误:
array:16 [
0 => "2016-07-05"
1 => "2016-07-12"
2 => "2016-07-19"
3 => "2016-07-26"
4 => "2016-08-02"
5 => "2016-08-09"
6 => "2016-08-16"
7 => "2016-08-23"
8 => "2016-08-30"
9 => "2016-09-06"
10 => "2016-09-13"
11 => "2016-09-20"
12 => "2016-09-27"
13 => "2016-10-03" --> should be 10-04
14 => "2016-10-10" --> should be 10-11
15 => "2016-10-17" --> should be 10-18
]
另一个例子:
array:16 [
0 => "2016-12-01"
1 => "2016-12-08"
2 => "2016-12-15"
3 => "2016-12-22"
4 => "2016-12-29"
5 => "2017-01-05"
6 => "2017-01-12"
7 => "2017-01-19"
8 => "2017-01-26"
9 => "2017-02-02"
10 => "2017-02-09"
11 => "2017-02-16"
12 => "2017-02-23"
13 => "2017-03-02"
14 => "2017-03-06" --> should be 03-09
15 => "2017-03-13" --> should be 03-16
]
以下是我所做的功能:
首先,我将请求从控制器传递给模型。然后我把初始日期分成初始年,月和日。现在我正致力于每周重复课程的限制。我将初始日期增加7,考虑是否超过该月份的天数,并在需要时增加该月份和年份。我制作的每个日期都被推入上面的数组中。你能帮忙找到一个缺失的步骤,让日期在循环结束时开始吗?
public static function repeat_array($request){
$repeat = $request->get('repeat_event');
$iteration = $request->get('repeat_iteration');
$initial = $request->get('date');
$mySets = Packages::my_sets();
$myReps = Packages::my_reps();
$initial_Y = date('Y',strtotime($initial));
$initial_M = date('m',strtotime($initial));
$initial_D = date('d',strtotime($initial));
$days_in_month = date('t',strtotime($initial_Y.'-'.$initial_M.'-01'));
$current_count = CalenderEvents::class_count();
$class_limit = $mySets*$myReps;
if($current_count >= $class_limit){
dd('too many');
} else{
if($repeat == 'weekly'){
if($iteration == 'Null'){
$weekly_array = [];
$loop_date = $initial_D;
$loop_month = $initial_M;
$loop_year = $initial_Y;
for($i = 0; $i < $class_limit; $i++){ -> //loop through the dates until limit is reached.
if($i == 0){ --> push the initial date as the first value in array.
array_push($weekly_array, $initial);
} else{ --> increment the day by 7.
$loop_date = $loop_date + 7;
if ($loop_date > $days_in_month){ --> //when the dates surpasses the amount of days in the month increment the month and subtract 30 from the total to get correct date.
$loop_date = $loop_date - 30;
$loop_month++;
if($loop_month > 12){
$loop_month = 1;
$loop_year++;
$loop_date = $loop_date - 1;
$date = date('Y-m-d',strtotime($loop_year.'-'.$loop_month.'-'.$loop_date));
array_push($weekly_array, $date);
} else{
$loop_date = $loop_date - 1;
$date = date('Y-m-d',strtotime($loop_year.'-'.$loop_month.'-'.$loop_date));
array_push($weekly_array, $date);
}
} else{
$date = date('Y-m-d',strtotime($loop_year.'-'.$loop_month.'-'.$loop_date));
array_push($weekly_array, $date);
}
}
}
dd($weekly_array);
}
}
$remainder = $class_limit - $current_count;
}
return true;
}
答案 0 :(得分:1)
你可以在php中使用relative formats和日期来制作插值日期。
使用DateTime类
初始化您的日期$date = new DateTime($initial)
将日期格式化为数组
$weekly_array[] = $date->format('Y-m-d')
和每次迭代
$date = $date->modify('+1 week');
$weekly_array[] = $date->format('Y-m-d')
如ideone example所示,这很好地处理了到明年的通道。