CRON作业-为不同时区安排Laravel命令-管理用户本地时间 [英] CRON JOB - Schedule Laravel command for different timezones - Manage users local time
问题描述
我使用的是 Laravel 5.4
,我希望在星期四
的晚上8点
I'm using Laravel 5.4
and I like to send notifications to users on thursdays
at 8pm
假设我有
- 东京的用户A
- 伦敦的用户B
- 纽约市的用户C
问题
我应该如何设置CRON作业,以便他们都在当地时间下午8点收到通知?
我的服务器在纽约市
$ schedule-> command('sendNotifications')-> dailyAt('20:00')->星期四()
推荐答案
仅使用以下一项无法完成此逻辑: $ schedule-> command('sendNotifications')
命令和一些参数.
This Logic can't be done just with just one: $schedule->command('sendNotifications')
command and some parameters.
第一步(如果尚未完成),请设置服务器并添加一个调用Laravel artisan命令的Cron作业:
First step, if not already done, setup your server and add a Cron job that is calling the Laravel artisan command:
* * * * * php/您的项目路径/工匠计划:运行>>/dev/null 2>& 1
第二步,将您的调度命令更改为:
Second step change your schedule command to:
$ schedule-> command('sendNotifications')-> everyFifteenMinutes();
或 $ schedule-> command('sendNotifications')-> everyThirtyMinutes();
或 $ schedule-> command('sendNotifications')-> hourly();
取决于您要使用它的精确度,因为某些时区有30或45分钟的偏移量,请参见 https://www.timeanddate.com/time/time-zones-interesting.html
depending how exact you want to have it, because some time zones have 30 or 45 Minutes offset, see https://www.timeanddate.com/time/time-zones-interesting.html
现在,如果执行了 sendNotifications
命令,则必须在此处建立逻辑:
Now if your command sendNotifications
is executed you have to build your logic there:
- 检查是否在世界某个地方是星期四,如果没有取消的话
- 获取当前为20:00的时区/县,如果必填
- 获取该时区/县之一中的所有用户并发送给他们的通知
您当然可以编写40多种不同的命令,每个时区一个,
并像这样使用调度程序:
Of course you can write 40+ different commands, one for each time zone,
and use the scheduler like this:
$schedule->command('sendNotifications_1')
->timezone('America/Chicago')
->thursdays()
->dailyAt('20:00');
$schedule->command('sendNotifications_2')
->timezone('Europe/London')
->thursdays()
->dailyAt('20:00');
$schedule->command('sendNotifications_3')
->timezone('Asia/Tokyo')
->thursdays()
->dailyAt('20:00');
这篇关于CRON作业-为不同时区安排Laravel命令-管理用户本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!