我做了一个自定义命令,希望它在上个月的最后一天 02:00 运行,但我也希望它在之后的某个时间段(如 02:00 到 15:00)之间运行,以下是我的调度程序
`$schedule->command('billing:generate')
->monthlyOn(Carbon::now()->endOfMonth()->subHours(5)->format("d"), "02:00");`
现在我认为实现这一目标的方法是这样的:
$schedule->command('billing:generate')
->monthlyOn(Carbon::now()->endOfMonth()->subHours(5)->format("d"), "02:00")
->between("02:00", "15:00");
它会按我的意愿工作吗?我在 laravel 版本 6。
当你在 Laravel 6 上时,你可以安排你的命令每天运行,但只需检查那一天是一个月的最后一天,如下所示:
// Runs exactly on "02:00" every month on its last day.
// (Actually runs every day, but doesn't execute if the day is not the last day of the month).
$schedule->command('billing:generate')->dailyAt('02:00')->when(function () {
return Carbon::now()->endOfMonth()->isToday();
});
// Runs between "02:00" and "15:00" every month on its last day.
// (Actually runs every day, but doesn't execute if the day is not the last day of the month).
$schedule->command('billing:generate')->daily()->between("02:00", "15:00")->when(function () {
return Carbon::now()->endOfMonth()->isToday();
});
如果你使用的是较新版本的 Laravel,你可以使用lastDayOfMonth
它来提供时间,如果需要,你也可以链接between
它。
此外,为了实现您想要的,您可以指定 2 个计划并解决问题。
它应该是这样的:
// Runs exactly on "02:00" every month on its last day.
$schedule->command('billing:generate')->lastDayOfMonth("02:00");
// Runs between "02:00" and "15:00" every month on its last day.
$schedule->command('billing:generate')->lastDayOfMonth()->between("02:00", "15:00");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句