PHP code example of nephifey / cron-expression

1. Go to this page and download the library: Download nephifey/cron-expression 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/ */

    

nephifey / cron-expression example snippets


# ┌───────────── minute (0–59)
# │ ┌───────────── hour (0–23)
# │ │ ┌───────────── day of the month (1–31)
# │ │ │ ┌───────────── month (1–12)
# │ │ │ │ ┌───────────── day of the week (0–6) (Sunday to Saturday;
# │ │ │ │ │                                   7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>



$parser = new \CronExpression\Parser();
$expression = $parser->parse("* * * * *");

$isDue = $expression->isDue();
$nextDateFormatted = $expression->getNextRunDate()->format("Y-m-d H:i:s");
$prevDateFormatted = $expression->getPrevRunDate()->format("Y-m-d H:i:s");

echo "Is Due: " . ($isDue ? "Yes" : "No") . "\r\n";
echo "Next Date: {$nextDateFormatted}\r\n";
echo "Prev Date: {$prevDateFormatted}\r\n";

if ($expression->isDue()) {
    echo "-- Run important code here --\r\n";
}

# crontab.php
  "* * * * *" => [
        function () { echo "-- Run important code here every minute --\r\n"; },
        function () { echo "-- Run important code here every minute #2 --\r\n"; },
    ],
    "*/5 * * * *" => [
        function () { echo "-- Run important code here every five minutes --\r\n"; },
    ],
];

foreach ($jobs as $expression => $callables) {
    $parser = $parser ?? new \CronExpression\Parser();
    if ($parser->parse($expression)->isDue())
        $combinedCallables = array_merge(($combinedCallables ?? []), $callables);
}

foreach (($combinedCallables ?? []) as $callable) {
    call_user_func($callable);
}



$parser = new \CronExpression\Parser();
$expression = $parser->parse("* * * * *", new DateTime("now", new DateTimeZone("America/Chicago")));

$isDue = $expression->isDue();
echo "Is Due: " . ($isDue ? "Yes" : "No") . "\r\n"; // Yes

$isDue = $expression->isDue(new DateTime("now", new DateTimeZone("America/New_York")));
echo "Is Due: " . ($isDue ? "Yes" : "No") . "\r\n"; // Yes

$isDue = $expression->isDue(new DateTime("now", new DateTimeZone("America/New_York")), true);
echo "Is Due: " . ($isDue ? "Yes" : "No") . "\r\n"; // No



$parser = new \CronExpression\Parser();
$expression = $parser->parse("* * * * *", new DateTime("now", new DateTimeZone("America/Chicago")));

echo "{$expression->getNextRunDate()->format("Y-m-d H:i:s")}\r\n"; // current time

$expression->reparse(new DateTime("now +1 minute", new DateTimeZone("America/Chicago")));

echo "{$expression->getNextRunDate()->format("Y-m-d H:i:s")}\r\n"; // one minute ahead

$newExpression = $expression->reparseImmutable(new DateTime("now +2 minute", new DateTimeZone("America/Chicago")));

echo "{$expression->getNextRunDate()->format("Y-m-d H:i:s")}\r\n"; // one minute ahead
echo "{$newExpression->getNextRunDate()->format("Y-m-d H:i:s")}\r\n"; // two minutes ahead

* * * * * apache php crontab.php > /dev/null 2>&1