MonthlyOn() 可以在 laravel 调度程序中使用吗?

发布于 2025-01-16 01:05:50 字数 546 浏览 0 评论 0原文

我制作了一个自定义命令,并希望它在上个月的最后一天 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。

I have made a custom command and want it to run on month last day at 02:00 but I also want it to run after that between some period of time like 02:00 till 15:00 following is my scheduler

`$schedule->command('billing:generate')
                ->monthlyOn(Carbon::now()->endOfMonth()->subHours(5)->format("d"), "02:00");`

now what I think Ill do to achieve this is like this:

$schedule->command('billing:generate')
            ->monthlyOn(Carbon::now()->endOfMonth()->subHours(5)->format("d"), "02:00")
            ->between("02:00", "15:00");

will it work as I want? I am on laravel version 6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天邊彩虹 2025-01-23 01:05:50

当您使用 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< /code> 您可以提供一个时间,如果需要,您还可以在其上链接 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");

As you are on Laravel 6, you can schedule your command to run every day, but just check is that day the last day of the month, something like this:

// 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();
});

If you are on newer versions of Laravel you can use lastDayOfMonth to which you can provide a time, and if needed, you can also chain between on it.

Also, to achieve what you want, you can specify 2 schedules and resolve the problem.

It should be like this:

// 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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文