PHP code example of carlossosa88 / cron-expression

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

    

carlossosa88 / cron-expression example snippets




rks with predefined scheduling definitions
$cron = Cron\CronExpression::factory('@daily');
$cron->isDue();
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');

// Works with complex expressions
$cron = Cron\CronExpression::factory('3-59/15 6-12 */15 1 2-5');
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');

// Calculate a run date two iterations into the future
$cron = Cron\CronExpression::factory('@daily');
echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s');

// Calculate a run date relative to a specific time
$cron = Cron\CronExpression::factory('@monthly');
echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s');



// Run a task every 5 seconds
$e = CronExpression::factory('* * * * * */5');
$this->assertTrue(
    $e->isDue(
        new DateTime('2014-04-07 00:00:05'), // Instance of datetime or 'now'
        null, // Time zone
        false // Drop seconds? Drop seconds parameter have to be set to FALSE, otherwise the default behavior is to reset to 0 current seconds 
    )
);

// Run a task on seconds 0,5,10,20
$e = CronExpression::factory('* * * * * 0,5,10,20');
$this->assertSame('2018-04-07 00:00:20',
    $e->getNextRunDate('2018-04-07 00:00:13', 0, false, null, false)
        ->format('Y-m-d H:i:s')
);