我正在开发一个项目,该项目需要安排事件并能够在特定的时间长度内重复这些事件。我的目标是传递表单中的初始日期并将其增加 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
]
这是我制作的函数:
首先,我将请求从 Controller 传递到模型。然后我获取初始日期并将其分解为初始年、月和日。现在我正在根据可用类(class)的限制进行每周重复。我将初始日期增加 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;
}
最佳答案
您可以使用relative formats使用 php 中的日期来插入日期。
使用 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 所示。 .
关于Php Laravel 日期数组循环逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254824/