00001 <?php
00039 class PhpCronTab
00040 {
00044 const EVERY_MINUTE = '*/1 * * * *';
00045 const EVERY_5_MINUTE = '*/5 * * * *';
00046 const EVERY_15_MINUTE = '*/15 * * * *';
00047 const EVERY_30_MINUTE = '*/30 * * * *';
00048 const EVERY_HOUR = '* */1 * * *';
00049 const EVERY_DAY = '0 21 * * *';
00050 const EVERY_WEEK = '0 22 * * 1';
00051
00055 public function __construct()
00056 {
00057 $this->scheduleList = array();
00058 $this->setCurrentTime(time());
00059 }
00060
00110 public function addUnixFormat($defined_)
00111 {
00112 $arr = explode(' ', $defined_);
00113
00114
00115
00116
00117 $this->addUnixFormatEx($arr[0], $arr[1], $arr[2], $arr[3], $arr[4]);
00118 }
00119
00123 public function addUnixFormatEx($minute_, $hour_, $dayOfMonth_, $month_, $dayOfWeek_)
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 }
00134
00138 public function clearUnixFormat()
00139 {
00140 $this->scheduleList = array();
00141 }
00142
00148 public function isTimeToExecute()
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
00173 return true;
00174 }
00175 }
00176
00183 public function setCurrentTime($time_)
00184 {
00185 $this->_currentTime = $time_;
00186 }
00187
00192 public function getCurrentTime()
00193 {
00194 return $this->_currentTime;
00195 }
00196
00215 private function _getCurrentTimeArr()
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 }
00226
00239 private function _checkClock($currentClock_, $testClock_)
00240 {
00241 $testClockArr = array();
00242
00243
00244 if ($testClock_ == '*')
00245 {
00246 return true;
00247 }
00248
00249
00250 elseif ($currentClock_ == $testClock_)
00251 {
00252 return true;
00253 }
00254
00255
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
00271
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
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 }
00299
00311 private function _checkDayOfMonth($dayOfMonth_, $scheduleDayOfMonth_)
00312 {
00313 if ('*' == $scheduleDayOfMonth_ || $dayOfMonth_ == $scheduleDayOfMonth_)
00314 {
00315 return true;
00316 }
00317 else
00318 {
00319 return false;
00320 }
00321 }
00322
00334 private function _checkMonth($currentMonth_, $scheduleDMonth_)
00335 {
00336 if ('*' == $scheduleDMonth_ || $currentMonth_ == $scheduleDMonth_)
00337 {
00338 return true;
00339 }
00340 else
00341 {
00342 return false;
00343 }
00344 }
00345
00357 private function _checkDayOfWeek($dayOfWeek_, $scheduledDayOfWeek_)
00358 {
00359 if ('*' == $scheduledDayOfWeek_ || $dayOfWeek_ == $scheduledDayOfWeek_)
00360 {
00361 return true;
00362 }
00363 else
00364 {
00365 return false;
00366 }
00367 }
00368
00372 private $scheduleList;
00373 }
00374
00375 ?>