Php Laravel 日期数组循环逻辑
Posted
技术标签:
【中文标题】Php Laravel 日期数组循环逻辑【英文标题】:Php Laravel date array loop logic 【发布时间】:2016-05-17 05:46:50 【问题描述】:我正在开展一个项目,该项目需要安排事件并能够在特定时间长度内重复它们。我的目标是传递表单中的初始日期并将其增加 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;
【问题讨论】:
【参考方案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所示。
【讨论】:
这太棒了!整天 +1 就是这么简单易懂! 当我第一次发现php的那个特性时,我和你一样热情。以上是关于Php Laravel 日期数组循环逻辑的主要内容,如果未能解决你的问题,请参考以下文章
循环 PHP 嵌套数组 - 将值提取到刀片视图中 (Laravel)