PHP code example of wolfcode / php-cron

1. Go to this page and download the library: Download wolfcode/php-cron library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wolfcode / php-cron example snippets



PhpCron\Scheduler;
use PhpCron\Timezone;

Scheduler::run(function (Scheduler $s) {
    $s->call(function () {
        echo date('Y-m-d H:i:s') . " heartbeat\n";
    })->minute(1)->name('heartbeat');

    $s->command('df -h')
        ->hourly()
        ->appendOutputTo(__DIR__ . '/disk-usage.log');

    $s->call(function () {
        // Send daily report at 9 AM on weekdays
    })->dailyAt('9:00')->weekdays()->name('daily-report');
}, Timezone::AMERICA_NEW_YORK);

use PhpCron\Timezone;

// Global timezone: all date() + status time + scheduling use Eastern time
Scheduler::run(function (Scheduler $s) {
    $s->call(function () {
        file_put_contents('log.txt', date('Y-m-d H:i:s') . "\n", FILE_APPEND);
    })->minute(1);
}, Timezone::AMERICA_NEW_YORK);

// Per-task timezone: only affects when this task triggers, not date()
$s->call(fn() => doSomething())
    ->timezone(Timezone::AMERICA_NEW_YORK)
    ->dailyAt('9:00');

->cron('0 9 * * 1')     // Every Monday at 9 AM
->cron('*/5 * * * * *') // Every 5 seconds (6-field)

->between('9:00', '17:00')          // Only between 9 AM and 5 PM
->unlessBetween('23:00', '6:00')    // Skip during specified hours
->when(fn() => someCondition())      // Run when truthy
->skip(fn() => holidayCheck())       // Skip when truthy
->environments('production')         // Only in given environments
->timezone(Timezone::ASIA_SHANGHAI)  // Per-task timezone

->before(function () { /* before task */ })
->after(function () { /* after task (always) */ })
->onSuccess(function ($output) { /* on success */ })
->onFailure(function (\Throwable $e) { /* on failure */ })

->withoutOverlapping()           // Prevent overlapping runs
->sendOutputTo('/path/to.log')   // Write output to file
->appendOutputTo('/path/to.log') // Append output to file
->name('task-name')              // Task identifier
bash
composer