9951 explained code solutions for 126 technologies


php-symfonyHow to set up a scheduler in Symfony with PHP?


Symfony provides a powerful way to set up a scheduler with PHP.

// Create a new CronJob instance
$job = new CronJob();

// Set the command to execute
$job->setCommand('php bin/console app:my-task');

// Set the schedule
$job->setSchedule('0 0 * * *');

// Save the job to the database
$job->save();

The above code will create a new CronJob instance, set the command to execute, set the schedule and save the job to the database.

The schedule is set using a cron expression, which is a string of five fields separated by spaces. The fields are:

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of Month (1-31)
  4. Month (1-12)
  5. Day of Week (0-6)

For more information, please refer to the Symfony documentation.

Edit this code on GitHub