Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ajvanho's avatar
Level 14

CarbonPeriod - by days of the week

How to make a period by days of the week. Instead every 3rd day from example, to be every monday, friday for that period ?

$period = CarbonPeriod::create(Carbon::now(), '3 days',  Carbon::now()->addMonths(3));

// In an ideal world it would be like this :)
$period = CarbonPeriod::create(Carbon::now(), [Carbon::MONDAY, Carbon::FRIDAY],  Carbon::now()->addMonths(3));
0 likes
15 replies
MichalOravec's avatar
$period = CarbonPeriod::between(now(), now()->addMonths(5))->filter('isWeekday');
1 like
Ajvanho's avatar
Level 14

I tried this, but: call_user_func() expects parameter 1 to be a valid callback, function 'isMonday' not found or invalid function name...˙

$period = CarbonPeriod::between(now(), now()->addMonths(5))->filter('isMonday');
MichalOravec's avatar

@ivanradojevic Ok for monday and friday

$period = CarbonPeriod::between(now(), now()->addMonth())->addFilter(function ($date) {
    return $date->isMonday() || $date->isFriday();
});
1 like
thinkverse's avatar
Level 15

If you want every Monday through Friday, you can use what @michaloravec wrote, if you want just the Mondays and Fridays you can check for isMonday() and isFriday() in your filter.

$period = CarbonPeriod::create(now(), now()->addMonths(3))
    ->filter(function (Carbon $date) {
        return $date->isMonday() || $date->isFriday();
    });

And you can verify that the filter works by dumping the englishDayOfWeek on the dates.

collect($period->map(function (Carbon $date) {
    return $date->englishDayOfWeek;
}))->dd();
1 like
Ajvanho's avatar
Level 14
$period = CarbonPeriod::create(now(), now()->addMonths(3))
    ->filter(function (Carbon $date) {
        return $date->isMonday() || $date->isFriday();
    });

This works for me

thinkverse's avatar

What do you mean doesn't work? I just ran it when I posted it. 🤔 If you want proof here are some screenshots of the code and output, ignore the phpcs warnings.

Screenshot of code

Screenshot of output

https://imgur.com/a/QfOkda8

Ajvanho's avatar
Level 14

OK, sorry.

That is the problem, when English is not my native language.

All the best!

Ajvanho's avatar
Level 14

@thinkverse This Carbon function is little tricky. I don't know how to pass the certain days off the week from the outside. I dont want hard-coded days of week. Is there any array that could be inserted into the filter? I know that my example like this is impossible, but I can't handle the carbon function.

$ddd = [1,5];
$filterdays = array();

$period = CarbonPeriod::create(now(), now()->addMonths(3))
    ->filter(function (Carbon $date) use($ddd) {
    	foreach($ddd as $d){
    		if ($d = 1) {
         		$filterdays[] =  $date->isMonday();
    		}
    		if ($d = 5) {
         		$filterdays[] = $date->isFriday();
    		}
    	}
    	return $filterdays;
    });

MichalOravec's avatar

@ivanradojevic It has to be like this

$days = [1, 5];

$period = CarbonPeriod::between(now(), now()->addMonths(3))->addFilter(function ($date) use ($days) {
    return in_array($date->dayOfWeekIso, $days);
});
3 likes
Ajvanho's avatar
Level 14

Wooow, thank you very much!!!

This deserves the best answer, but it would not be right to change.

I'll give it next time!

karresachi's avatar

what if I want to add times too. what should the function look like?

Please or to participate in this conversation.