Laravel scheduler timezone
Asked Answered
A

1

9

How do I set the Laravel scheduler to run a task at a specific time in a certain timezone? The server is set to UTC, but I want to run the task at 12 noon every Monday in the Pacific/Auckland timezone.

E.g. this will work, but the timezone is UTC:

protected function schedule(Schedule $schedule) {
    $schedule->command('run-report')->weekly()->mondays()->at('12:00'));
}
Appositive answered 14/1, 2016 at 22:54 Comment(0)
S
21

You can do it using timezone() method:

$schedule->command('run-report')->weekly()->mondays()->at('12:00')->timezone('Pacific/Auckland');
Swag answered 14/1, 2016 at 23:10 Comment(3)
It would be hard to put all methods in the Laravel guide, but you can always check the API docs (e.g. laravel.com/api/5.1/Illuminate/Console/Scheduling/Event.html) or just browse the codeSwag
Hmm, so I tried this, left it for 2 weeks, but it never got triggered.Appositive
when you call timezone() it just sets the timezone that is later applied to a DateTime object being used to compare current time with what you set in the scheduler - it can only impact the time, but won't prevent the command from running. I can't really tell why your task was not executed - you need to do some debugging on your own. Make sure that schedule:run command was run, check if there are no errors in the log files, maybe add some logging on your ownSwag

© 2022 - 2024 — McMap. All rights reserved.