Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检索一个月内的所有特定天数_Php_Laravel_Laravel 5_Php Carbon - Fatal编程技术网

Php 检索一个月内的所有特定天数

Php 检索一个月内的所有特定天数,php,laravel,laravel-5,php-carbon,Php,Laravel,Laravel 5,Php Carbon,我试图从过去的某个特定日期起,用碳来检索每个月的所有周一、周二和最后一个周日。 然后,我想循环遍历天数数组,并为当天创建一个偶数。我在想怎么做 // Get all dates of Mondays, Tuesdays, last Sundays of every Month since the first Monday of January of 2000. $eventDates = Carbon::parse('first Monday of January 2000'); $i = 0

我试图从过去的某个特定日期起,用碳来检索每个月的所有周一、周二和最后一个周日。 然后,我想循环遍历天数数组,并为当天创建一个偶数。我在想怎么做

// Get all dates of Mondays, Tuesdays, last Sundays of every Month since the first Monday of January of 2000.
$eventDates = Carbon::parse('first Monday of January 2000');

$i = 0;
while($eventDates->lt(Carbon::now()->subDay(14))) {

    $event = factory(Event::class)->create([
        'date' => $eventDate,
    ]);
}

我确信有一种更有效的方法可以做到这一点,但我对碳不太熟悉。在这里,您将得到3个数组,自2000年以来每个月的每个星期一、星期二和最后一个星期天都有。然后,您可以迭代每个
日期周期
,或合并间隔以获得一个大的
日期周期
,以创建事件

<?php
use Carbon\Carbon;
use Carbon\CarbonInterval;
$monday = Carbon::parse('First Monday of January 2000');
$tuesday = Carbon::parse('First Tuesday of January 2000');
$sunday = Carbon::parse('Last Sunday of January 2000');
$now = Carbon::now();
$mondays = new DatePeriod(
    $monday,
    CarbonInterval::week(),
    $now
);
$tuesdays = new DatePeriod(
    $tuesday,
    CarbonInterval::week(),
    $now
);
$sundays = new DatePeriod(
    $sunday,
    CarbonInterval::week(4),
    $now
);

$allDays = [];
foreach ($mondays as $day) {
    $allDays[] = $day;
}
foreach ($tuesdays as $day) {
    $allDays[] = $day;
}
foreach ($sundays as $day) {
    $allDays[] = $day;
}
usort($allDays, function ($a, $b) {
    return strtotime($a) - strtotime($b);
});
foreach ($allDays as $day) {
    echo $day->format("M D Y-m-d")."<br>";
}
您可以使用Carbon的
filter()
isWeekday()
方法来设置日期周期:
$period=CarbonPeriod::介于('2020-03-01','2020-03-31')->过滤器('isWeekday');
foreach($期间为$日期){
$days[]=$date->format('Y-m-d');
}
回波内爆(“
”,$days);
这将打印出来

2020-03-02
2020-03-03
2020-03-04
2020-03-05
2020-03-06
2020-03-09
2020-03-10
2020-03-11
2020-03-12
2020-03-13
2020-03-16
2020-03-17
2020-03-18
2020-03-19
2020-03-20
2020-03-23
2020-03-24
2020-03-25
2020-03-26
2020-03-27
2020-03-30
2020-03-31

我尝试过使用collection方法merge,但由于某种原因,它没有合并。我打算尝试将所有内容合并到一个主数组中,然后按日期排序。我已经编辑了这篇文章。它甚至比以前更难看,但现在它正在将它们全部排序到一个数组中。@ishegg检查我的答案。这可能就是我们应该做这件事的方式。