Skip to content

Commit 2303c5e

Browse files
authored
Add daysOfMonth() method to schedule tasks on specific days (#57817)
- Allows scheduling tasks to run on multiple specific days of the month - Supports both array and variadic syntax: daysOfMonth(1, 15) or daysOfMonth([1, 15]) - Useful for tasks that need to run on specific dates (payroll, reports, etc.) - Add comprehensive tests
1 parent 46bd07c commit 2303c5e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Illuminate/Console/Scheduling/ManagesFrequencies.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,21 @@ public function lastDayOfMonth($time = '0:0')
568568
return $this->spliceIntoPosition(3, Carbon::now()->endOfMonth()->day);
569569
}
570570

571+
/**
572+
* Schedule the event to run on specific days of the month.
573+
*
574+
* @param array<int<1, 31>>|int<1, 31> ...$days
575+
* @return $this
576+
*/
577+
public function daysOfMonth(...$days)
578+
{
579+
$days = count($days) === 1 && is_array($days[0]) ? $days[0] : $days;
580+
581+
$this->dailyAt('0:0');
582+
583+
return $this->spliceIntoPosition(3, implode(',', $days));
584+
}
585+
571586
/**
572587
* Schedule the event to run quarterly.
573588
*

tests/Console/Scheduling/EventTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,16 @@ public function testCustomMutexName()
104104

105105
$this->assertSame('fancy-command-description', $event->mutexName());
106106
}
107+
108+
public function testDaysOfMonthMethod()
109+
{
110+
$event = new Event(m::mock(EventMutex::class), 'php -i');
111+
112+
$event->daysOfMonth(1, 15);
113+
$this->assertSame('0 0 1,15 * *', $event->getExpression());
114+
115+
$event = new Event(m::mock(EventMutex::class), 'php -i');
116+
$event->daysOfMonth([1, 10, 20, 30]);
117+
$this->assertSame('0 0 1,10,20,30 * *', $event->getExpression());
118+
}
107119
}

0 commit comments

Comments
 (0)