public function occursOn($date) { if (!Valid::dateTimeObject($date)) { throw new \InvalidArgumentException("occursOn: Accepts valid DateTime objects"); } // breakdown the date $year = $date->format('Y'); $month = $date->format('n'); $day = $date->format('j'); $dayFromEndOfMonth = -((int) $date->format('t') + 1 - (int) $day); $leapYear = (int) $date->format('L'); $yearDay = $date->format('z') + 1; $yearDayNeg = -366 + (int) $yearDay; if ($leapYear) { $yearDayNeg = -367 + (int) $yearDay; } // this is the nth occurrence of the date $occur = ceil($day / 7); $occurNeg = -1 * ceil(abs($dayFromEndOfMonth) / 7); // starting on a monday $week = $date->format('W'); $weekDay = strtolower($date->format('D')); $dayOfWeek = $date->format('l'); $dayOfWeekAbr = strtolower(substr($dayOfWeek, 0, 2)); // the date has to be greater then the start date if ($date < $this->startDate) { return false; } // if the there is an end date, make sure date is under if (isset($this->until)) { if ($date > $this->until) { return false; } } if (isset($this->bymonths)) { if (!in_array($month, $this->bymonths)) { return false; } } if (isset($this->bydays)) { if (!in_array(0 . $dayOfWeekAbr, $this->bydays) && !in_array($occur . $dayOfWeekAbr, $this->bydays) && !in_array($occurNeg . $dayOfWeekAbr, $this->bydays)) { return false; } } if (isset($this->byweeknos)) { if (!in_array($week, $this->byweeknos)) { return false; } } if (isset($this->bymonthdays)) { if (!in_array($day, $this->bymonthdays) && !in_array($dayFromEndOfMonth, $this->bymonthdays)) { return false; } } if (isset($this->byyeardays)) { if (!in_array($yearDay, $this->byyeardays) && !in_array($yearDayNeg, $this->byyeardays)) { return false; } } return true; }
public function occursOn($date) { if (!Valid::dateTimeObject($date)) { throw new \InvalidArgumentException("occursOn: Accepts valid DateTime objects"); } // breakdown the date $year = $date->format('Y'); $month = $date->format('n'); $day = $date->format('j'); $dayFromEndOfMonth = -((int) $date->format('t') + 1 - (int) $day); $leapYear = (int) $date->format('L'); $yearDay = $date->format('z') + 1; $yearDayNeg = -366 + (int) $yearDay; if ($leapYear) { $yearDayNeg = -367 + (int) $yearDay; } // this is the nth occurrence of the date $occur = ceil($day / 7); $occurNeg = -1 * ceil(abs($dayFromEndOfMonth) / 7); // starting on a monday $week = $date->format('W'); $weekDay = strtolower($date->format('D')); $dayOfWeek = $date->format('l'); $dayOfWeekAbr = strtolower(substr($dayOfWeek, 0, 2)); // the date has to be greater then the start date if ($date < $this->startDate) { return false; } // if the there is an end date, make sure date is under if (isset($this->until)) { if ($date > $this->until) { return false; } } if (isset($this->bymonths)) { if (!in_array($month, $this->bymonths)) { return false; } } if (isset($this->bydays)) { if (!in_array(0 . $dayOfWeekAbr, $this->bydays) && !in_array($occur . $dayOfWeekAbr, $this->bydays) && !in_array($occurNeg . $dayOfWeekAbr, $this->bydays)) { return false; } } if (isset($this->byweeknos)) { if (!in_array($week, $this->byweeknos)) { return false; } } if (isset($this->bymonthdays)) { if (!in_array($day, $this->bymonthdays) && !in_array($dayFromEndOfMonth, $this->bymonthdays)) { return false; } } if (isset($this->byyeardays)) { if (!in_array($yearDay, $this->byyeardays) && !in_array($yearDayNeg, $this->byyeardays)) { return false; } } // If there is an interval != 1, check whether this is an nth period. if ($this->interval > 1) { switch ($this->freq) { case 'yearly': $start = new \DateTime($this->startDate->format("Y-1-1\\TH:i:sP")); $sinceStart = $date->diff($start); $numPeriods = $sinceStart->y; break; case 'monthly': $start = new \DateTime($this->startDate->format("Y-m-1\\TH:i:sP")); $sinceStart = $date->diff($start); $numYears = $sinceStart->y; $numMonths = $sinceStart->m; $numPeriods = $numYears * 12 + $numMonths; break; case 'weekly': if (isset($this->bydays)) { $weekStartDate = self::getFirstWeekStartDate($this->startDate, $this->wkst); } else { $weekStartDate = $this->startDate; } $sinceStart = $date->diff($weekStartDate); $numPeriods = floor($sinceStart->days / 7); break; case 'daily': $sinceStart = $date->diff($this->startDate); // Note we "expanded" startDate already. $numPeriods = $sinceStart->days; break; case 'hourly': $sinceStart = $date->diff($this->startDate); // Note we "expanded" startDate already. $numDays = $sinceStart->days; $numHours = $sinceStart->h; $numPeriods = 24 * $numDays + $numHours; break; case 'minutely': $sinceStart = $date->diff($this->startDate); // Note we "expanded" startDate already. $numDays = $sinceStart->days; $numHours = $sinceStart->h; $numMinutes = $sinceStart->i; $numPeriods = 60 * (24 * $numDays + $numHours) + $numMinutes; break; case 'secondly': $sinceStart = $date->diff($this->startDate); // Note we "expanded" startDate already. $numDays = $sinceStart->days; $numHours = $sinceStart->h; $numMinutes = $sinceStart->i; $numSeconds = $sinceStart->s; $numPeriods = 60 * (60 * (24 * $numDays + $numHours)) + $numMinutes + $numSeconds; break; } if ($numPeriods % $this->interval == 0) { return true; } else { return false; } } return true; }