public function testIsOnSameDayOfMonth() { $ts1 = 1424520000; // Saturday, 21-Feb-15 12:00:00 UTC $ts2 = 1426939200; // Saturday, 21-Mar-15 12:00:00 UTC $ts3 = 1425297600; // Monday, 02-Mar-15 12:00:00 UTC $this->assertTrue($this->util->isOnSameDayOfMonth($ts1, $ts2)); $this->assertFalse($this->util->isOnSameDayOfMonth($ts1, $ts3)); }
/** * Calculates the end (or start) timestamp of the last event in a sequence of occurrences * * @param int $occurrences Max number of occurrences * @param int $from_timestamp Initial time to calculate from (defaults to event start time) * @param bool $at_event_end If true, will return the timestamp of the event end, otherwise event start * @return int */ public function calculateEndAfterTimestamp($occurrences = 1, $from_timestamp = null, $at_event_end = true) { $occurrences = (int) $occurrences; $start_timestamp = $this->getStartTimestamp(); $start_day = $start_timestamp; $test_day = $start_timestamp > $from_timestamp ? $start_timestamp : $from_timestamp; while ($occurrences > 0) { $shows = false; $offset_on_test_day = Util::getOffset($test_day, Util::UTC, $this->getTimezone()); // next increment $next_test_day = $test_day + Util::SECONDS_IN_A_DAY; switch ($this->repeat_frequency) { default: $occurrences = 0; break; case Util::FREQUENCY_DAILY: $shows = true; break; case Util::FREQUENCY_WEEKDAY: $repeat_weekly_days = array(Util::SATURDAY, Util::SUNDAY); foreach ($repeat_weekly_days as $key => $day) { $repeat_weekly_days[$key] = Util::getDayOfWeek(strtotime($day, $test_day) - $offset_on_test_day); } $D = Util::getDayOfWeek($test_day); $shows = !in_array($D, $repeat_weekly_days); break; case Util::FREQUENCY_WEEKDAY_ODD: $repeat_weekly_days = array(Util::MONDAY, Util::WEDNESDAY, Util::FRIDAY); foreach ($repeat_weekly_days as $key => $day) { $repeat_weekly_days[$key] = Util::getDayOfWeek(strtotime($day, $test_day) - $offset_on_test_day); } $D = Util::getDayOfWeek($test_day); $shows = in_array($D, $repeat_weekly_days); break; case Util::FREQUENCY_WEEKDAY_EVEN: $repeat_weekly_days = array(Util::TUESDAY, Util::THURSDAY); foreach ($repeat_weekly_days as $key => $day) { $repeat_weekly_days[$key] = Util::getDayOfWeek(strtotime($day, $test_day) - $offset_on_test_day); } $D = Util::getDayOfWeek($test_day); $shows = in_array($D, $repeat_weekly_days); break; case Util::FREQUENCY_WEEKLY: $repeat_weekly_days = $this->repeat_weekly_days; if (!$repeat_weekly_days) { $repeat_weekly_days = Util::getDayOfWeek($this->getStartTimestamp()); } if (!is_array($repeat_weekly_days)) { $repeat_weekly_days = array($repeat_weekly_days); } $D = Util::getDayOfWeek($test_day); foreach ($repeat_weekly_days as $key => $day) { // use offsets // Monday in Sydney can still be Sunday at UTC $repeat_weekly_days[$key] = Util::getDayOfWeek(strtotime($day, $test_day) - $offset_on_test_day); } $shows = in_array($D, $repeat_weekly_days); break; case Util::FREQUENCY_MONTHLY: if ($this->repeat_monthly_by == Util::REPEAT_MONTHLY_BY_DAY_OF_WEEK) { $shows = Util::isOnSameWeekDayOfMonth($test_day, $this->getStartTimestamp()); if ($shows) { // we can skip 4 weeks $next_test_day = strtotime('+28 days', $test_day); } } else { $shows = Util::isOnSameDayOfMonth($test_day, $this->getStartTimestamp()); if ($shows) { // we can skip a month $next_test_day = strtotime('+1 month', $test_day); } } break; case Util::FREQUENCY_YEARLY: $shows = Util::isOnSameDayOfYear($test_day, $this->getStartTimestamp()); if ($shows) { // we can skip a year $next_test_day = strtotime('+1 year', $test_day); } break; } if ($shows) { $occurrences--; $start_timestamp = (int) Util::getTimeOfDay($this->getStartTimestamp(), $test_day); } $test_day = $next_test_day; } return $at_event_end ? $start_timestamp + $this->end_delta : $start_timestamp; }