private function setNumDates() { $rrule = $this->state->get('event.rrule'); //get the number of dates in the billing period based on the recurrence and the start date. $timezone = JFactory::getConfig()->get('offset'); //use the event start date $this->state->get('event.rrule.dtstart'); $startDate = new \DateTime($rrule->dtstart, new \DateTimeZone($timezone)); //use the event end date $this->state->get('event.rrule.until'); $endDate = new \DateTime($rrule->until, new \DateTimeZone($timezone)); // Optional $rule = new \Recurr\Rule($rrule->toString(), $startDate, $endDate, $timezone); $transformer = new \Recurr\Transformer\ArrayTransformer(); $dateCollection = $transformer->transform($rule); }
public function getSchedule() { if (!$this->start_date || !$this->is_recurring || !$this->frequency_id) { return false; } $startDate = $this->getOriginal('last_sent_date') ?: $this->getOriginal('start_date'); $startDate .= ' ' . $this->account->recurring_hour . ':00:00'; $startDate = $this->account->getDateTime($startDate); $endDate = $this->end_date ? $this->account->getDateTime($this->getOriginal('end_date')) : null; $timezone = $this->account->getTimezone(); $rule = $this->getRecurrenceRule(); $rule = new \Recurr\Rule("{$rule}", $startDate, $endDate, $timezone); // Fix for months with less than 31 days $transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); $transformerConfig->enableLastDayOfMonthFix(); $transformer = new \Recurr\Transformer\ArrayTransformer(); $transformer->setConfig($transformerConfig); $dates = $transformer->transform($rule); if (count($dates) < 2) { return false; } return $dates; }
<input type="checkbox" name="testy" value="3"> <input type="checkbox" name="testy" value="4"> <input type="checkbox" name="testy" value="5"> <input type="submit"> </form> <div data-alert class="alert-box info"> <span class="fi-asterisk alertHeader" aria-hidden="true"></span><h2>DEBUGGING:</h2> <?php $timezone = 'America/Toronto'; $startDate = new \DateTime('2013-06-12 20:00:00', new \DateTimeZone($timezone)); $endDate = new \DateTime('2013-06-14 20:00:00', new \DateTimeZone($timezone)); // Optional $rule = new \Recurr\Rule('FREQ=MONTHLY;UNTIL=2015-07', $startDate, $endDate, $timezone); $transformer = new \Recurr\Transformer\ArrayTransformer(); $collection = $transformer->transform($rule); print_r($collection); echo "<br><br>"; print_r($collection[0]); echo "<br><br>"; print_r($collection[0]->getStart()->date); echo "<br><br>"; foreach ($collection as $r) { echo $r->getStart()->date . "<br>"; } ?> <a href="#" class="close">×</a> </div>
function get_recurr_dates($post_id) { if (!class_exists('Recurrence')) { //--Doctrine require_once $this->path . 'recurr/Doctrine/Common/Collections/Collection.php'; require_once $this->path . 'recurr/Doctrine/Common/Collections/Selectable.php'; require_once $this->path . 'recurr/Doctrine/Common/Collections/ArrayCollection.php'; //--Rule require_once $this->path . 'recurr/Exception.php'; require_once $this->path . 'recurr/Exception/InvalidRRule.php'; require_once $this->path . 'recurr/Exception/InvalidWeekday.php'; require_once $this->path . 'recurr/Exception/MissingData.php'; require_once $this->path . 'recurr/Exception/InvalidArgument.php'; require_once $this->path . 'recurr/Rule.php'; require_once $this->path . 'recurr/Transformer/ArrayTransformer.php'; require_once $this->path . 'recurr/Transformer/ArrayTransformerConfig.php'; require_once $this->path . 'recurr/DateUtil.php'; require_once $this->path . 'recurr/Frequency.php'; require_once $this->path . 'recurr/Weekday.php'; require_once $this->path . 'recurr/DateInfo.php'; require_once $this->path . 'recurr/DaySet.php'; require_once $this->path . 'recurr/Time.php'; require_once $this->path . 'recurr/Recurrence.php'; require_once $this->path . 'recurr/RecurrenceCollection.php'; require_once $this->path . 'recurr/DateExclusion.php'; require_once $this->path . 'recurr/Exception.php'; require_once $this->path . 'recurr/Transformer/ConstraintInterface.php'; require_once $this->path . 'recurr/Transformer/Constraint.php'; require_once $this->path . 'recurr/Transformer/Constraint/BeforeConstraint.php'; } $rrule = $this->get_rrule($post_id); $start = get_post_meta($post_id, 'fc_start_datetime', true); $end = get_post_meta($post_id, 'fc_end_datetime', true); $fc_allday = intval(get_post_meta($post_id, 'fc_allday', true)); if (empty($start)) { return false; } //-- $timezone = $this->get_timezone(); $DateTimeZone = new \DateTimeZone($timezone); $ts = strtotime($start); $end_seconds = $ts + intval(apply_filters('rhc_recurr_limit_seconds', 157784760)); // hard limit of 5 years. $recurr_end_date = date("Y-m-d H:i:s", $end_seconds); try { $startDate = new \DateTime($start, $DateTimeZone); } catch (Exception $e) { return false; } //--- if ($fc_allday) { $startDate->setTime(0, 0, 0); } if (empty($end)) { $endDate = null; } else { try { $endDate = new \DateTime($end, $DateTimeZone); } catch (Exception $e) { return false; } if ($fc_allday) { $endDate->setTime(0, 0, 0); } $ts_end = strtotime($start); if ($endDate->format('U') < $startDate->format('U')) { $endDate = clone $startDate; } } if (empty($rrule)) { if (null == $endDate) { $constraint_endDate = clone $startDate; } else { $constraint_endDate = clone $endDate; //bug: dates like april 30, 2016 do not show in calendar. $constraint_endDate->add(DateInterval::createFromDateString('1 day')); } //$constraint_endDate = new \DateTime($start, $DateTimeZone ); } else { $constraint_endDate = new \DateTime($recurr_end_date, $DateTimeZone); } if ($endDate != null && $constraint_endDate->format('U') < $endDate->format('U')) { $constraint_endDate = clone $endDate; } //-- if (empty($rrule)) { $rrule = "FREQ=DAILY;INTERVAL=1;COUNT=1"; } //error_log("RRULE $rrule \n",3,ABSPATH.'api.log'); /* error_log( "startDate:".print_r($startDate,true)."\n",3,ABSPATH.'api.log'); error_log( "endDate:".print_r($endDate,true)."\n",3,ABSPATH.'api.log'); error_log( "constraint endDate:".print_r($constraint_endDate,true)."\n",3,ABSPATH.'api.log'); */ $rrule = str_replace("RRULE:", "", $rrule); try { $rule = new \Recurr\Rule($rrule, $startDate, $endDate, $timezone); } catch (Exception $error) { $rule = new \Recurr\Rule("FREQ=DAILY;INTERVAL=1;COUNT=1", $startDate, $endDate, $timezone); } $constraint = new \Recurr\Transformer\Constraint\BeforeConstraint($constraint_endDate, true); $transformer = new \Recurr\Transformer\ArrayTransformer(); $dates = $transformer->transform($rule, null, $constraint); //--- add repeat dates $duration = false; if ($endDate) { $duration = $startDate->diff($endDate); } $rdate = get_post_meta($post_id, 'fc_rdate', true); $rdate_arr = array(); if ('' != trim($rdate)) { $rdate_arr = explode(',', $rdate); if (count($rdate_arr) > 0) { foreach ($rdate_arr as $date_str) { $tmp_date_start = new \DateTime($date_str, new \DateTimeZone($timezone)); $tmp_date_end = null; if (false !== $duration) { $tmp_date_end = new \DateTime($date_str, new \DateTimeZone($timezone)); $tmp_date_end->add($duration); } $new_recur = new \Recurr\Recurrence($tmp_date_start, $tmp_date_end); $dates->add($new_recur); } //--- $iterator = $dates->getIterator(); $iterator->uasort(function ($a, $b) { return $a->getStart() < $b->getStart() ? -1 : 1; }); $dates = new \Recurr\RecurrenceCollection(iterator_to_array($iterator)); } } //--- exclude $exdate = get_post_meta($post_id, 'fc_exdate', true); $exdate_arr = array(); if (!$dates->isEmpty() && '' != trim($exdate)) { $exdate_arr = explode(',', $exdate); if (count($exdate_arr) > 0) { $exclude_date_objects = array(); foreach ($exdate_arr as $date_str) { $tmp_date = new \DateTime($date_str, new \DateTimeZone($timezone)); if (is_object($tmp_date)) { $exclude_date_objects[] = $tmp_date; } } foreach ($dates as $date) { if (in_array($date->getStart(), $exclude_date_objects)) { $dates->removeElement($date); } } } } return $dates; }
/** * Get dates array based on recur template. * * @return array */ public function getRecurDates($start, $rrule) { $timezone = "UTC"; //"UTC"; //'America/New_York' 'America/Denver' craft()->getTimeZone() //convert time back to selected timezone. $startDateString = new \DateTime($start->format('c'), new \DateTimeZone(craft()->getTimeZone())); //$startDateString = $start->format(DateTime::MYSQL_DATETIME, DateTime::UTC); // $rule = new \Recurr\Rule($rrule, $startDateString, null, $timezone); $transformer = new \Recurr\Transformer\ArrayTransformer(); $transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); $transformerConfig->enableLastDayOfMonthFix(); $transformer->setConfig($transformerConfig); return $transformer->transform($rule); }
/** * Get a list of group pages * * @param string $rtrn What data to return * @param array $filters Filters to apply to data retrieval * @param boolean $boolean Clear cached data? * @return mixed */ public function events($rtrn = 'list', $filters = array(), $clear = false) { switch (strtolower($rtrn)) { case 'count': if (!$this->_events_count || $clear) { $tbl = new Tables\Event($this->_db); $this->_events_count = $tbl->count($filters); } return $this->_events_count; break; case 'repeating': if (!$this->_events_repeating instanceof ItemList || $clear) { // var to hold repeating data $repeats = array(); // add repeating filters $filters['repeating'] = true; // capture publish up/down // remove for now as we want all events that have a repeating rule $start = Date::of($filters['publish_up']); $end = Date::of($filters['publish_down']); unset($filters['publish_up']); unset($filters['publish_down']); // find any events that match our filters $tbl = new Tables\Event($this->_db); if ($results = $tbl->find($filters)) { foreach ($results as $key => $result) { $start = Date::of($result->publish_up); // get the repeating & pass start date $rule = new \Recurr\Rule($result->repeating_rule, $start); // define constraint that date must be between event publish_up & end $constraint = new \Recurr\Transformer\Constraint\BetweenConstraint($start, $end); // create transformmer & generate occurances $transformer = new \Recurr\Transformer\ArrayTransformer(); $occurrences = $transformer->transform($rule, null, $constraint); // calculate diff so we can create down $diff = new DateInterval('P0Y0DT0H0M'); if ($result->publish_down != '0000-00-00 00:00:00') { $diff = date_diff(Date::of($result->publish_up), Date::of($result->publish_down)); } // create new event for each reoccurrence foreach ($occurrences as $occurrence) { $event = clone $result; $event->publish_up = $occurrence->getStart()->format('Y-m-d H:i:s'); $event->publish_down = $occurrence->getStart()->add($diff)->format('Y-m-d H:i:s'); $repeats[] = new Event($event); } } } $this->_events_repeating = new ItemList($repeats); } return $this->_events_repeating; break; case 'list': default: if (!$this->_events instanceof ItemList || $clear) { $tbl = new Tables\Event($this->_db); if ($results = $tbl->find($filters)) { foreach ($results as $key => $result) { $results[$key] = new Event($result); } } $this->_events = new ItemList($results); } return $this->_events; break; } }
/** * @param int $eventDateUid */ public function updateRecurrences($eventDateUid) { $date = $this->eventDateRepository->findByUid($eventDateUid); if (!$date) { /* Do not update hidden records – they'll be updated when the are activated again */ return; } if ($date->getFrequency() === 0) { /* Remove recurrences that may have existed before switching to frequency=0 */ $this->removeRecurrences($eventDateUid); return; } if ($date->getStart()) { $startDate = $date->getStart(); // Timezone is set to '+01:00' by default (with a European default timzeone) // set the timezone explictly to make DateTransitions work $startDate->setTimezone(new \DateTimeZone(date_default_timezone_get())); $endDate = null; if ($date->getEnd()) { $endDate = $date->getEnd(); $endDate->setTimezone(new \DateTimeZone(date_default_timezone_get())); } if (!in_array($date->getFrequency(), array_keys(self::$freqs))) { return; } $rrule = ['FREQ' => self::$freqs[$date->getFrequency()]]; if ($date->getFrequencyUntil()) { $rrule['UNTIL'] = $date->getFrequencyUntil()->format(\DateTime::ATOM); } else { $rrule['COUNT'] = $date->getFrequencyCount(); } if ($rrule['FREQ'] == 'WEEKLY') { $byday = $this->buildByDay($date->getFrequencyWeekdays()); if ($byday) { $rrule['BYDAY'] = $byday; } } $rule = new \Recurr\Rule($rrule, $startDate, $endDate); $transformer = new \Recurr\Transformer\ArrayTransformer(); $transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); $limit = $this->configurationService->get('recurrence_virtual_limit'); if ($limit) { $transformerConfig->setVirtualLimit($limit); } $transformerConfig->enableLastDayOfMonthFix(); $transformer->setConfig($transformerConfig); $this->mergeUpdatesWithExistingRecurrences($date, $transformer->transform($rule)); } }
<?php use yii\helpers\Html; use yii\bootstrap\ActiveForm; use dosamigos\datetimepicker\DateTimePicker; $summary = ''; if ($model->recurrence_rule) { $rule = new \Recurr\Rule($model->recurrence_rule); $transformer = new \Recurr\Transformer\ArrayTransformer(); $transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); $transformerConfig->enableLastDayOfMonthFix(); $transformer->setConfig($transformerConfig); $textTransformer = new \Recurr\Transformer\TextTransformer(new \Recurr\Transformer\Translator('en')); $summary = $textTransformer->transform($rule); } ?> <?php $form = ActiveForm::begin(['id' => 'event-form', 'layout' => 'horizontal', 'fieldConfig' => ['template' => "{label}\n{beginWrapper}\n{input}\n{endWrapper}", 'horizontalCssClasses' => ['label' => 'col-sm-3', 'offset' => 'col-sm-offset-3', 'wrapper' => 'col-sm-8']]]); ?> <?php echo Html::activeHiddenInput($model, 'id'); ?> <?php echo Html::activeHiddenInput($model, 'calendar_id'); echo Html::activeHiddenInput($model, 'created_by'); echo Html::activeHiddenInput($model, 'recurrence_rule'); ?>
public static function getDatesByEventID($id, $start, $end) { $events = array(); $evt = CalendarEvent::findOne($id); //Só envia os eventos privados do próprio utilizador if ($evt->event_type == \app\enum\EventType::Privado && $evt->calendar_id != \Yii::$app->user->identity->calendar_id) { return $events; } if ($evt->recurrence != 1) { $events[] = CalendarEvent::getEventArray($evt); } else { $timezone = date_default_timezone_get(); $evtstart = new \DateTime($evt->start); $evtend = new \DateTime($evt->end); $rrule = $evt->recurrence_rule; $rule = new \Recurr\Rule($rrule, $evtstart, $evtend, $timezone); $transformer = new \Recurr\Transformer\ArrayTransformer(); //$transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); //$transformerConfig->enableLastDayOfMonthFix(); //$transformer->setConfig($transformerConfig); //$constraint = new \Recurr\Transformer\Constraint\BetweenConstraint($rstart,$rend,true); //$results = $transformer->transform($rule,null,$constraint); $results = $transformer->transform($rule); // $textTransformer = new \Recurr\Transformer\TextTransformer(new \Recurr\Transformer\Translator('en')); // echo 'eventos entre '. $start . ' e '. $end .'<br>'; // echo $evt->recurrence_rule .'<br>'; // echo $textTransformer->transform($rule) .'<br>'; // // var_dump($results->startsBetween(new \DateTime($start),new \DateTime($end),true)); die; foreach ($results->startsBetween(new \DateTime($start), new \DateTime($end), true) as $obj) { $s = $obj->getStart(); $e = $obj->getEnd(); $evt->setEventDateTime($s->format('Y-m-d H:i'), $e->format('Y-m-d H:i')); $events[] = CalendarEvent::getEventArray($evt); } } return $events; }
/** * Get list of events. * @param \core_kernel_classes_Resource $delivery - main delivery instance * @return \Recurr\RecurrenceCollection */ public function getRecurrenceCollection(\core_kernel_classes_Resource $delivery) { $deliveryProps = $delivery->getPropertiesValues(array(new \core_kernel_classes_Property(TAO_DELIVERY_START_PROP), new \core_kernel_classes_Property(TAO_DELIVERY_END_PROP), new \core_kernel_classes_Property(DeliveryScheduleService::TAO_DELIVERY_RRULE_PROP))); $propStartExec = current($deliveryProps[TAO_DELIVERY_START_PROP]); $propEndExec = current($deliveryProps[TAO_DELIVERY_END_PROP]); $rrule = !empty($deliveryProps[DeliveryScheduleService::TAO_DELIVERY_RRULE_PROP]) ? current($deliveryProps[DeliveryScheduleService::TAO_DELIVERY_RRULE_PROP])->literal : false; if (!empty($rrule)) { $startDate = date_create('@' . $propStartExec->literal); $endDate = date_create('@' . $propEndExec->literal); $diff = date_diff($startDate, $endDate); $rule = new \Recurr\Rule((string) $rrule); $transformer = new \Recurr\Transformer\ArrayTransformer(); $rEvents = $transformer->transform($rule); unset($rEvents[0]); //the first recurrence has the same time as the main delivery foreach ($rEvents as $rEvent) { $end = clone $rEvent->getStart(); $end->add($diff); $rEvent->setEnd($end); } } else { $rEvents = array(); } return $rEvents; }
public function getDatesBetween(DateTime $startDate = null, DateTime $endDate = null, $extend = false, $inc = false) { //working with a local copy so we don't have to track dates when used multiple time $rrule = clone $this; if (isset($startDate)) { $rrule->dtstart = strftime('%Y%m%dT000000Z', $startDate->getTimestamp()); } else { //format the unix epoch $rrule->dtstart = strftime('%Y%m%dT000000Z', $rrule->dtstart); } if (!isset($endDate) || isset($rrule->until) && $rrule->until < strtotime($endDate->getTimestamp()) && !$extend) { $rrule->until = strftime('%Y%m%dT000000Z', $rrule->until); } else { $rrule->until = strftime('%Y%m%dT000000Z', $endDate->getTimestamp()); //strftime('%Y%m%d',$endDate); } $rrule_string = $rrule->toString(); //get the number of dates in the billing period based on the recurrence and the start date. $timezone = JFactory::getConfig()->get('offset'); //use the event start date $this->state->get('event.rrule.dtstart'); //$startDate = new \DateTime(strftime('%Y%m%d',$datestart), new \DateTimeZone($timezone)); //use the event end date $this->state->get('event.rrule.until'); //$endDate = new \DateTime(strftime('%Y%m%d',$datestop), new \DateTimeZone($timezone)); // Optional JLoader::registerNamespace('Recurr', JPATH_LIBRARIES); JLoader::registerNamespace('Doctrine', JPATH_LIBRARIES); $rule = new Recurr\Rule($rrule_string, $startDate, $endDate, $timezone); $transformer = new Recurr\Transformer\ArrayTransformer(); if (isset($startDate)) { $constraint = new Recurr\Transformer\Constraint\AfterConstraint($startDate, true); } else { $constraint = null; } $dateCollection = $transformer->transform($rule, 30, $constraint); return $dateCollection; }
/** * Get dates array based on recur template. * * @return array */ public function getRecurDates($start, $rrule) { $timezone = craft()->getTimeZone(); //'UTC','America/New_York','America/Denver' craft()->getTimeZone() $startDateString = $start->format(DateTime::MYSQL_DATETIME); #-- returns null or datetime $endOn = craft()->venti_rule->getEndOn($rrule); $rule = new \Recurr\Rule($rrule, $startDateString, $endOn, $timezone); $transformer = new \Recurr\Transformer\ArrayTransformer(); $transformerConfig = new \Recurr\Transformer\ArrayTransformerConfig(); $transformerConfig->enableLastDayOfMonthFix(); $transformer->setConfig($transformerConfig); // if ($endOn !== null) // { // $constraint = new \Recurr\Transformer\Constraint\BetweenConstraint($start, $endOn, true); // } // else // { // $constraint = new \Recurr\Transformer\Constraint\AfterConstraint(new \DateTime(), true); // } return $transformer->transform($rule); }
/** * Get all deliveries in time range. * @param integer $from Timestamp * @param integer $to Timestamp * @return core_kernel_classes_Resource[] - delivery resource instances */ public function getAssemblies($from, $to) { $assemblies = DeliveryAssemblyService::singleton()->getAllAssemblies(); $startProp = new \core_kernel_classes_Property(TAO_DELIVERY_START_PROP); $endProp = new \core_kernel_classes_Property(TAO_DELIVERY_END_PROP); $result = array(); $timeZone = new \DateTimeZone('UTC'); $filterStartDate = \DateTime::createFromFormat('U', $from, $timeZone); $filterEndDate = \DateTime::createFromFormat('U', $to, $timeZone); foreach ($assemblies as $delivery) { $deliveryProps = $delivery->getPropertiesValues(array($startProp, $endProp, new \core_kernel_classes_Property(DeliveryScheduleService::TAO_DELIVERY_RRULE_PROP))); if (empty($deliveryProps[TAO_DELIVERY_START_PROP]) || empty($deliveryProps[TAO_DELIVERY_END_PROP])) { continue; } $deliveryStartTs = (int) current($deliveryProps[TAO_DELIVERY_START_PROP])->literal; $deliveryEndTs = (int) current($deliveryProps[TAO_DELIVERY_END_PROP])->literal; $rrule = (string) current($deliveryProps[DeliveryScheduleService::TAO_DELIVERY_RRULE_PROP]); if (empty($rrule)) { if ($deliveryStartTs < $from && $deliveryEndTs < $from || $deliveryStartTs > $to && $deliveryEndTs > $to) { continue; } $result[] = $delivery; } else { $rule = new \Recurr\Rule($rrule); $transformer = new \Recurr\Transformer\ArrayTransformer(); $rEvents = $transformer->transform($rule)->startsBetween($filterStartDate, $filterEndDate); if (count($rEvents) !== 0) { $result[] = $delivery; } } } return $result; }