Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lifesound's avatar

Laravel Scheduler DateTime error

I create that schedule in Kernel

protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();

        $schedule->call(function () {
            Notification::create([
                'message' => sprintf('Item '),
                'model_type' => 'item',
                'model_id' => 4000,
                'user_id' => 5000,
            ]);

            $deadItemsDays = Setting::where('name', 'period_for_any_items_notification')->first()->value;
            $deadNewItems = Setting::where('name', 'period_for_new_items_notification')->first()->value;

            dd($deadItemsDays);

            $items = Item::query()
            ->where('closed', false)
            ->where('suspended', false)
            ->where('on_hold', false)
            ->whereHas('actions', function ($query) {
                $query->whereDate('created_at', '<=', now()->subDays($this->getDeadItemsDays())->startOfDay());
            })
            ->get();

            $technicians = User::role('technician')->get();

            foreach ($items as $item) {
                foreach ($technicians as $technician) {
                    Notification::create([
                        'message' => sprintf('Item %has no actions for %d', $item->id, $deadNewItems),
                        'model_type' => 'item',
                        'model_id' => $item->id,
                        'user_id' => $technician->id,
                    ]);
                }
            }

            $new_items = Item::query()
            ->where('new', true)
            ->whereHas('actions', function ($query) {
                $query->whereDate('created_at', '<=', now()->subDays($this->getDeadNewItemsDays())->startOfDay());
            })
            ->get();

            $technicians = User::role('technician')->get();

            foreach ($new_items as $item) {
                foreach ($technicians as $technician) {
                    Notification::create([
                        'message' => sprintf('Item %has no actions for %d', $item->id, $deadItemsDays),
                        'model_type' => 'item',
                        'model_id' => $item->id,
                        'user_id' => $technician->id,
                    ]);
                }
            }
        })->everyMinute();
    }

when commanding php artisan schedule:list I have an err :


   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(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

HOW COULD I TEST THAT ? HOW to run it ?

0 likes
14 replies
tykus's avatar

Is there a value in config('app.timezone')?

lifesound's avatar

@tykus

    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();

        $schedule->call(function () {
            Notification::create([
                'message' => sprintf('Item '),
                'model_type' => 'item',
                'model_id' => 4000,
                'user_id' => 5000,
            ]);

            $deadItemsDays = Setting::where('name', 'period_for_any_items_notification')->first()->value;
            $deadNewItems = Setting::where('name', 'period_for_new_items_notification')->first()->value;

            dd($deadItemsDays);

            $items = Item::query()
            ->where('closed', false)
            ->where('suspended', false)
            ->where('on_hold', false)
            ->whereHas('actions', function ($query) {
                $query->whereDate('created_at', '<=', now()->subDays($this->getDeadItemsDays())->startOfDay());
            })
            ->get();

            $technicians = User::role('technician')->get();

            foreach ($items as $item) {
                foreach ($technicians as $technician) {
                    Notification::create([
                        'message' => sprintf('Item %has no actions for %d', $item->id, $deadNewItems),
                        'model_type' => 'item',
                        'model_id' => $item->id,
                        'user_id' => $technician->id,
                    ]);
                }
            }

            $new_items = Item::query()
            ->where('new', true)
            ->whereHas('actions', function ($query) {
                $query->whereDate('created_at', '<=', now()->subDays($this->getDeadNewItemsDays())->startOfDay());
            })
            ->get();

            $technicians = User::role('technician')->get();

            foreach ($new_items as $item) {
                foreach ($technicians as $technician) {
                    Notification::create([
                        'message' => sprintf('Item %has no actions for %d', $item->id, $deadItemsDays),
                        'model_type' => 'item',
                        'model_id' => $item->id,
                        'user_id' => $technician->id,
                    ]);
                }
            }
        })->everyMinute();
    }

This is the whole func

tykus's avatar

@lifesound I see nothing obvious... stumped.

If you go into the ScheduleListCommand and dd($this->option('timezone')), what do you get? Then ``dd($this->option('timezone', config('app.timezone')))`?

Snapey's avatar

Do you know if your server has 'Africa/Cairo' locale installed?

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Can you try a composer update

1 like

Please or to participate in this conversation.