public function getNewEventOnArbitraryDate(EventModel $event, \DateTime $newDate) { $timeZoneUTC = new \DateTimeZone("UTC"); $timeZone = new \DateTimeZone($this->timeZoneName); $start = clone $newDate; $start->setTimezone($timeZone); $start->setTime($event->getStartAtInTimezone()->format('G'), $event->getStartAtInTimezone()->format('i'), $event->getStartAtInTimezone()->format('s')); $start->setTimezone($timeZoneUTC); $end = clone $start; $end->add($event->getStartAtInUTC()->diff($event->getEndAtInUTC(), true)); $newEvent = new EventModel(); $newEvent->setGroupId($event->getGroupId()); $newEvent->setVenueId($event->getVenueId()); $newEvent->setCountryId($event->getCountryId()); $newEvent->setEventRecurSetId($this->id); $newEvent->setSummary($event->getSummary()); $newEvent->setDescription($event->getDescription()); $newEvent->setStartAt($start); $newEvent->setEndAt($end); foreach ($this->customFields as $customField) { if ($event->hasCustomField($customField)) { $newEvent->setCustomField($customField, $event->getCustomField($customField)); } } return $newEvent; }
/** * * Gets new monthly events on the basis that the event is on the something day in the week. * eg. 2nd tuesday of month * eg. 4th saturday in month * * @param \models\EventModel $event * @param type $monthsInAdvance * @return \models\EventModel */ public function getNewMonthlyEventsOnSetDayInWeek(EventModel $event, $daysInAdvance = 186) { // constants $interval = new \DateInterval('P1D'); $timeZone = new \DateTimeZone($this->timeZoneName); $timeZoneUTC = new \DateTimeZone("UTC"); // calculate which day of month it should be $dayOfWeek = $event->getStartAt()->format("N"); $thisStart = new \DateTime($event->getStartAt()->format('Y-m-d H:i:s'), $timeZoneUTC); $thisEnd = new \DateTime($event->getEndAt()->format('Y-m-d H:i:s'), $timeZoneUTC); $thisStart->setTimeZone($timeZone); $thisEnd->setTimeZone($timeZone); $weekInMonth = 1; while ($thisStart->format('d') > 1) { $thisStart->sub($interval); $thisEnd->sub($interval); if ($thisStart->format("N") == $dayOfWeek) { ++$weekInMonth; } } // vars $out = array(); $currentMonthLong = $thisStart->format('F'); $currentMonthShort = $thisStart->format('M'); $currentMonth = $thisStart->format('m'); $currentWeekInMonth = 1; $loopStop = \TimeSource::time() + $daysInAdvance * 24 * 60 * 60; while ($thisStart->getTimestamp() < $loopStop) { $thisStart->add($interval); $thisEnd->add($interval); //print $thisStart->format("r")." current month: ".$currentMonth." current week: ".$currentWeekInMonth."\n"; if ($currentMonth != $thisStart->format('m')) { $currentMonth = $thisStart->format('m'); $currentWeekInMonth = 1; } if ($thisStart->format("N") == $dayOfWeek) { if ($currentWeekInMonth == $weekInMonth && $thisStart->getTimestamp() > \TimeSource::time()) { $start = clone $thisStart; $end = clone $thisEnd; $start->setTimeZone($timeZoneUTC); $end->setTimeZone($timeZoneUTC); $include = true; if ($include) { $newEvent = new EventModel(); $newEvent->setGroupId($event->getGroupId()); $newEvent->setVenueId($event->getVenueId()); $newEvent->setCountryId($event->getCountryId()); $newEvent->setEventRecurSetId($this->id); $newEvent->setSummary($event->getSummary()); $newEvent->setDescription($event->getDescription()); $newEvent->setStartAt($start); $newEvent->setEndAt($end); foreach ($this->customFields as $customField) { if ($event->hasCustomField($customField)) { $newEvent->setCustomField($customField, $event->getCustomField($customField)); } } if (stripos($newEvent->getSummary(), $currentMonthLong) !== false) { $newEvent->setSummary(str_ireplace($currentMonthLong, $newEvent->getStartAt()->format('F'), $newEvent->getSummary())); } else { if (stripos($newEvent->getSummary(), $currentMonthShort) !== false) { $newEvent->setSummary(str_ireplace($currentMonthShort, $newEvent->getStartAt()->format('M'), $newEvent->getSummary())); } } $out[] = $newEvent; } } ++$currentWeekInMonth; } } return $out; }