I am trying to make cron jobs with Laravel 8
$schedule->command('myCommand')->daily()->timezone('Europe/Paris');
I have an error when I run on server
php artisan schedule:list
php artisan schedule:list
TypeError
DateTime::setTimezone(): Argument #1 ($timezone) must be of type DateTimeZone, null given
at vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleListCommand.php:42
38▕ $event->expression,
39▕ $event->description,
40▕ (new CronExpression($event->expression))
41▕ ->getNextRunDate(Carbon::now()->setTimezone($event->timezone))
➜ 42▕ ->setTimezone($this->option('timezone', config('app.timezone')))
43▕ ->format('Y-m-d H:i:s P'),
44▕ ];
45▕ }
46▕
+14 vendor frames
15 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
And
php artisan schedule:run
No scheduled commands are ready to run.
Tried:
TypeError
DateTime::setTimezone(): Argument #1 ($timezone) must be of typ
...
e(Carbon::now()->setTimezone($event->timezone))
➜ 42▕ ->setTimezone($this->option('timezone', config('Europe/Paris')))
43▕ ->format('Y-m-d H:i:s P'),
44▕ ];
45▕
Illuminate\Foundation\Console\Kernel::handle()
My Answer:
command('my:command')->daily()->timezone('America/New_York');
This code snippet sets the timezone for the Laravel scheduler to 'America/New_York' when scheduling a daily cron job. This ensures that the cron job runs at the correct time according to the specified timezone.
Rate this post
3 of 5 based on 4879 votesComments