简体   繁体   中英

PHP's CarbonPeriod put all data from the interval into one array

So I have this code to get the data from the database:

// get custom holidays
$companyHolidays = Holiday::query()->get();

now to get this output I did dd($companyHolidays->toArray());

在此处输入图像描述

these start and end dates need to be put into a DateInterval using Carbon.

So I did this:

$parsedCompanyHolidays = CarbonPeriod::create($companyHolidays[0]->start, $companyHolidays[0]->end);

this outputs, using dd($parsedCompanyHolidays->toArray()) ;

在此处输入图像描述

the issue now is that it only shows the first Osterferien but not the other ones from the database? What are ways to get all of them into one array?

Because later I would need to continue a generation of dates that are inside these dates

                   while ($i < $eventRepeats) {

                        // add one week
                        $eventStart->addDays(7);


                        if (in_array($eventStart->translatedFormat('Y-m-d'), $holidaysArray)) {
                            continue;
                        }

                        // todo: not grabbing the holidays correctly
//                        if (in_array($eventStart->translatedFormat('Y-m-d'), $parsedCompanyHolidays)) {
//                            continue;
//                        }


                        echo '<div class="text-primary font-normal mt-6 mb-2">'.$count++.'. Termin</div>';
                        echo '<div class="bg-gray rounded-lg border-l-4">';
                        echo '<div class="p-3">';
                        echo $eventStart->translatedFormat('l, d. F Y, H:i');
                        echo ' - ';
                        echo $eventEnd->translatedFormat('H:i');
                        echo '</div></div>';

                        // increase iteration
                        $i++;

                    }

:

$companyHolidays = Holiday::all();
$newholiday = [];
foreach($companyHolidays as $holiday){
  $newholiday[] = [
     'id' => $holiday->id,
     'title' => $holiday->title,
     'start' => $holiday->start,
     'end' => $holiday->end,
     'parsed_company_holidays'=> CarbonPeriod::create($holiday->start, $holiday->end);
        ];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM