CRON JOB - Schedule Laravel command for different timezones - Manage users local time

CRON JOB - Schedule Laravel command for different timezones - Manage users local time

我正在使用 Laravel 5.4,我喜欢在 thursdays 8pm

上向用户发送通知

假设我有

  1. 东京的用户 A
  2. 伦敦的用户 B
  3. 纽约市的用户 C

问题

我应该如何设置 CRON JOB 以便他们都在当地时间 8pm 收到通知?

我的服务器在纽约市

代码

$schedule->command('sendNotifications')->dailyAt('20:00')->thursdays()

嗯,我不知道 laravel 是否有这个功能。

但是您可以设置每个小时的 cron 作业来检查用户当前时间(带时区)是否是早上 8 点,以及我们是否没有向他们发送通知。

您可以向这些用户发送通知。

这个逻辑不能只用一个:$schedule->command('sendNotifications')命令和一些参数。

第一步,如果尚未完成,请设置您的服务器并添加一个调用 Laravel artisan 命令的 Cron 作业:

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

选项一

第二步将您的日程安排命令更改为:

$schedule->command('sendNotifications')->everyFifteenMinutes();$schedule->command('sendNotifications')->everyThirtyMinutes();$schedule->command('sendNotifications')->hourly();

取决于您想要的精确度,因为有些时区有 30 或 45 分钟的偏移量,请参阅 https://www.timeanddate.com/time/time-zones-interesting.html

现在,如果你的命令 sendNotifications 被执行,你必须在那里构建你的逻辑:

  • 检查世界某个地方是否是星期四,如果不是则取消
  • 得到timezones/counties此刻20:00的位置,容差如果 需要
  • 让所有用户加入其中一个 timezones/counties 并发送 给他们的通知

选项二

当然你可以编写 40 多个不同的命令,每个时区一个,
并像这样使用调度程序:

$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');