can you use $dt->isSunday(); and then filter those from your results?
Nov 5, 2021
8
Level 2
Filter weekdays + saturday Carbon
Hello Guys,
I have array with days of week.
[
{
"date": "2021-11-01"
},
{
"date": "2021-11-02"
},
{
"date": "2021-11-03"
},
{
"date": "2021-11-04"
},
{
"date": "2021-11-05"
},
{
"date": "2021-11-06"
},
{
"date": "2021-11-07"
}
]
Any I get this date by this code:
CarbonPeriod::create(
2021-11-01,
2021-11-07
);
I know I can use filter method filter('isWeekday') but I will only receive weekdays: 2021-11-01 - 2021-11-05. How I can get this array with weekdays + saturday?
Thanks!
Level 54
@Sinres you can filter out the Sundays as follows:
$dates = CarbonPeriod::since('2021-11-01')->days(1)->until('2021-11-07');
$datesWithoutSundays = collect($dates)->filter(function ($date) {
return !$date->isSunday();
})->map(function ($date) {
return ['date' => $date->format('Y-m-d')];
});
returns:
[
{
"date": "2021-11-01"
},
{
"date": "2021-11-02"
},
{
"date": "2021-11-03"
},
{
"date": "2021-11-04"
},
{
"date": "2021-11-05"
},
{
"date": "2021-11-06"
}
]
1 like
Please or to participate in this conversation.