PhpCronTab Class Reference
[General]

Scheduling functionality similiar to the unix crontab. More...

List of all members.

Public Member Functions

 __construct ()
 Constructor.
 addUnixFormat ($defined_)
 Add a new time period when isTimeExecuted should be return true.
 addUnixFormatEx ($minute_, $hour_, $dayOfMonth_, $month_, $dayOfWeek_)
 See addUnixFormat.
 clearUnixFormat ()
 Clear all added schedules.
 isTimeToExecute ()
 Check if it's time to execute the program.
 setCurrentTime ($time_)
 Set the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
 getCurrentTime ()
 Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Public Attributes

const EVERY_MINUTE = '*/1 * * * *'
 Parameters for PhpCronTab::addUnixFormat().
const EVERY_5_MINUTE = '*/5 * * * *'
const EVERY_15_MINUTE = '*/15 * * * *'
const EVERY_30_MINUTE = '*/30 * * * *'
const EVERY_HOUR = '* */1 * * *'
const EVERY_DAY = '0 21 * * *'
const EVERY_WEEK = '0 22 * * 1'

Private Member Functions

 _getCurrentTimeArr ()
 Return array with current time.
 _checkClock ($currentClock_, $testClock_)
 Check the hour or minute field for crontab syntax.
 _checkDayOfMonth ($dayOfMonth_, $scheduleDayOfMonth_)
 Check if current day of month matches with the scheduled day of month.
 _checkMonth ($currentMonth_, $scheduleDMonth_)
 Check if current month matches with scheduled month.
 _checkDayOfWeek ($dayOfWeek_, $scheduledDayOfWeek_)
 Check if current day of week matches with the scheduled day of week.

Private Attributes

 $scheduleList
 List of scheduled times.


Detailed Description

Scheduling functionality similiar to the unix crontab.

This class is used to set a time period when something should be executed. The code needs to be combined with other code that will trigger the PhpCronTab::isTimeToExecute() atleast once a minute. For example in unix crontab.

The following example will do database check everyday day 21:00, and optimize database once a week.

/var/www/batches/index.php

 include_once('FareOfficeLib/FareOfficeLib.php');
 $phpCronTab = new PhpCronTab;
 $phpCronTab->addUnixFormat(EVERY_DAY);
 if ($phpCronTab->isTimeToExecute())
 {
   // Check table user fast;
 }

 $phpCronTab->clearUnixFormat()
 $phpCronTab->addUnixFormat(EVERY_WEEK);
 if ($phpCronTab->isTimeToExecute())
 {
   // optimize table user;
 }

Then add this to /etc/crontab

 0 21 * * * root /usr/bin/php /var/www/batches/index.php

Author:
Daniel Lindh <[email protected]>

Definition at line 39 of file PhpCronTab.class.php.


Constructor & Destructor Documentation

PhpCronTab::__construct (  ) 

Constructor.

Definition at line 55 of file PhpCronTab.class.php.

References setCurrentTime().

00056   {
00057     $this->scheduleList = array();
00058     $this->setCurrentTime(time());
00059   }


Member Function Documentation

PhpCronTab::_checkClock ( currentClock_,
testClock_ 
) [private]

Check the hour or minute field for crontab syntax.

Parameters:
$currentClock_ - string - Either the current hour or current minute. Should not begin with 0.
$testClock_ - string - The hour or minute when the execution should start.
Returns:
- bool - true if execution should be done.

Definition at line 239 of file PhpCronTab.class.php.

Referenced by isTimeToExecute().

00240   {
00241     $testClockArr = array();
00242 
00243     // * always execute this.
00244     if ($testClock_ == '*')
00245     {
00246       return true;
00247     }
00248 
00249     // If same value
00250     elseif ($currentClock_ == $testClock_)
00251     {
00252       return true;
00253     }
00254 
00255     // Ranged values, 1-10 means all values from 1 to 10.
00256     elseif (strpos($testClock_, '-') !== false)
00257     {
00258       $numberRange = explode('-', $testClock_);
00259       if
00260       (
00261         count($numberRange) == 2 &&
00262         $currentClock_ >= $numberRange[0] &&
00263         $currentClock_ <= $numberRange[1]
00264       )
00265       {
00266         return true;
00267       }
00268     }
00269 
00270     // Will rebuild to real values in an array.
00271     // */3 will be 0,3,6,9,12,15,18,21 etc.
00272     elseif (strpos($testClock_, '/') !== false)
00273     {
00274       $skipNumber = explode('/', $testClock_);
00275       if ($skipNumber[0] == '*')
00276       {
00277         for($i = 0; $i < 60; $i+=$skipNumber[1])
00278         {
00279           $testClockArr[$i] = true;
00280         }
00281       }
00282     }
00283 
00284     // 1,3,7,9 will be expanded to an array.
00285     elseif (strpos($testClock_, ',') !== false)
00286     {
00287       $testClockArr = explode(',', $testClock_);
00288       $testClockArr = array_flip($testClockArr);
00289     }
00290 
00291     //
00292     if (!empty($testClockArr) && array_key_exists($currentClock_,  $testClockArr))
00293     {
00294       return true;
00295     }
00296 
00297     return false;
00298   }

PhpCronTab::_checkDayOfMonth ( dayOfMonth_,
scheduleDayOfMonth_ 
) [private]

Check if current day of month matches with the scheduled day of month.

Parameters:
$dayOfMonth_ - string - Current day of month, 0-6, 0 is sunday.
$scheduleDayOfMonth_ - string - The scheduled day of month.
Returns:
- bool - true if execution should be done.

Definition at line 311 of file PhpCronTab.class.php.

00312   {
00313     if ('*' == $scheduleDayOfMonth_ || $dayOfMonth_ == $scheduleDayOfMonth_)
00314     {
00315       return true;
00316     }
00317     else
00318     {
00319       return false;
00320     }
00321   }

PhpCronTab::_checkDayOfWeek ( dayOfWeek_,
scheduledDayOfWeek_ 
) [private]

Check if current day of week matches with the scheduled day of week.

Parameters:
$dayOfWeek_ - string - Current day of week, 0-6, 0 is sunday.
$scheduledDayOfWeek_ - string - The scheduled day of week.
Returns:
- bool - true if execution should be done.

Definition at line 357 of file PhpCronTab.class.php.

00358   {
00359     if ('*' == $scheduledDayOfWeek_ || $dayOfWeek_ == $scheduledDayOfWeek_)
00360     {
00361       return true;
00362     }
00363     else
00364     {
00365       return false;
00366     }
00367   }

PhpCronTab::_checkMonth ( currentMonth_,
scheduleDMonth_ 
) [private]

Check if current month matches with scheduled month.

Parameters:
$currentMonth_ - string - Current month
$scheduleDMonth_ - string - The scheduled month.
Returns:
- bool - true if execution should be done.

Definition at line 334 of file PhpCronTab.class.php.

00335   {
00336     if ('*' == $scheduleDMonth_ || $currentMonth_ == $scheduleDMonth_)
00337     {
00338       return true;
00339     }
00340     else
00341     {
00342       return false;
00343     }
00344   }

PhpCronTab::_getCurrentTimeArr (  )  [private]

Return array with current time.

Example return array.

 array
 (
   'minute' => 23,
   'hour'=> 14,
   'day_of_month' = 2,
   'month' => 2,
   'day_of_week = 5'
 );

Current time without leading zeroes and zero based. Hour is between 0-23 for example.

Definition at line 215 of file PhpCronTab.class.php.

References getCurrentTime().

Referenced by isTimeToExecute().

00216   {
00217     return array
00218     (
00219       'minute'        => ltrim(date('i', $this->getCurrentTime()), '0'),
00220       'hour'          => ltrim(date('G', $this->getCurrentTime()), '0'),
00221       'day_of_month'  => ltrim(date('d', $this->getCurrentTime()), '0'),
00222       'month'         => ltrim(date('n', $this->getCurrentTime()), '0'),
00223       'day_of_week'   => ltrim(date('w', $this->getCurrentTime()), '0')
00224     );
00225   }

PhpCronTab::addUnixFormat ( defined_  ) 

Add a new time period when isTimeExecuted should be return true.

Each add follows a particular format as a series of fields, separated by spaces and/or tabs. Each field can have a single value or a series of values.

OPERATORS There are several ways of specifying multiple date/time values in a field:

  • The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"
  • The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"
  • The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour'.
  • There is also an operator which some extended versions of cron support, the slash ('/') operator, which can be used to skip a given number of values. For example, "* /3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21"; "*" specifies 'every hour' but the "/3" means that only the first, fourth, seventh... and such values given by "*" are used.

Fields +---------------- minute (0 - 59) | +------------- hour (0 - 23) | | +---------- day of month (1 - 31) | | | +------- month (1 - 12) | | | | +---- day of week (0 - 7) (Sunday=0 or 7) | | | | | * * * * *

Each of the patterns from the five fields may be either * (an asterisk), meaning all legal values, or a list of elements separated by commas.

For "day of the week" (field 5), 0 are considered Sunday.

Counterintuitively, if both "day of month" (field 3) and "day of week" (field 5) are present on the same line, then the command is executed when either is true.

Parameters:
$defined_ See the function comment.

Definition at line 110 of file PhpCronTab.class.php.

References addUnixFormatEx().

00111   {
00112     $arr = explode(' ', $defined_);
00113 
00114     // @todo
00115     //assertLog(count($arr) == 5, EL_LEVEL_3, ECAT_DIAGNOSTIC, 'Not the right number of arguments: ' . count($arr) . ' ' . $defined_);
00116 
00117     $this->addUnixFormatEx($arr[0], $arr[1], $arr[2], $arr[3], $arr[4]);
00118   }

PhpCronTab::addUnixFormatEx ( minute_,
hour_,
dayOfMonth_,
month_,
dayOfWeek_ 
)

See addUnixFormat.

Definition at line 123 of file PhpCronTab.class.php.

Referenced by addUnixFormat().

00124   {
00125     $this->scheduleList[] = array
00126     (
00127       'minute'        => ltrim($minute_, '0'),
00128       'hour'          => ltrim($hour_, '0'),
00129       'day_of_month'  => ltrim($dayOfMonth_, '0'),
00130       'month'         => ltrim($month_, '0'),
00131       'day_of_week'   => ltrim($dayOfWeek_, '0')
00132     );
00133   }

PhpCronTab::clearUnixFormat (  ) 

Clear all added schedules.

Definition at line 138 of file PhpCronTab.class.php.

00139   {
00140     $this->scheduleList = array();
00141   }

PhpCronTab::getCurrentTime (  ) 

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Definition at line 192 of file PhpCronTab.class.php.

Referenced by _getCurrentTimeArr().

00193   {
00194     return $this->_currentTime;
00195   }

PhpCronTab::isTimeToExecute (  ) 

Check if it's time to execute the program.

Returns:
bool

Definition at line 148 of file PhpCronTab.class.php.

References _checkClock(), and _getCurrentTimeArr().

00149   {
00150     if (!empty($this->scheduleList))
00151     {
00152       $currentTime = $this->_getCurrentTimeArr();
00153 
00154       foreach ($this->scheduleList as $schedule)
00155       {
00156         if
00157         (
00158           $this->_checkClock($currentTime['minute'], $schedule['minute']) &&
00159           $this->_checkClock($currentTime['hour'], $schedule['hour']) &&
00160           $this->_checkClock($currentTime['day_of_month'], $schedule['day_of_month']) &&
00161           $this->_checkClock($currentTime['month'], $schedule['month']) &&
00162           $this->_checkClock($currentTime['day_of_week'], $schedule['day_of_week'])
00163         )
00164         {
00165           return true;
00166         }
00167       }
00168       return false;
00169     }
00170     else
00171     {
00172       // If no sceduleList are configured, always execute
00173       return true;
00174     }
00175   }

PhpCronTab::setCurrentTime ( time_  ) 

Set the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Parameters:
$time_ 

Definition at line 183 of file PhpCronTab.class.php.

Referenced by __construct().

00184   {
00185     $this->_currentTime = $time_;
00186   }


Member Data Documentation

PhpCronTab::$scheduleList [private]

List of scheduled times.

Definition at line 372 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_15_MINUTE = '*/15 * * * *'

Definition at line 46 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_30_MINUTE = '*/30 * * * *'

Definition at line 47 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_5_MINUTE = '*/5 * * * *'

Definition at line 45 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_DAY = '0 21 * * *'

Definition at line 49 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_HOUR = '* */1 * * *'

Definition at line 48 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_MINUTE = '*/1 * * * *'

Parameters for PhpCronTab::addUnixFormat().

Definition at line 44 of file PhpCronTab.class.php.

const PhpCronTab::EVERY_WEEK = '0 22 * * 1'

Definition at line 50 of file PhpCronTab.class.php.


The documentation for this class was generated from the following file:

Generated on Wed May 6 23:28:24 2009 for fareofficelib by  doxygen 1.5.8