/** * Return a preload interval based on the chosen timeline interval and the given date and time * * @param DateTime $dateTime The date and time to use * * @return DateInterval The interval to pre-load */ private function getPreloadInterval(DateTime $dateTime) { switch ($this->view->intervalBox->getInterval()) { case '1d': return DateInterval::createFromDateString('1 week -1 second'); case '1w': return DateInterval::createFromDateString('8 weeks -1 second'); case '1m': $dateCopy = clone $dateTime; for ($i = 0; $i < 6; $i++) { $dateCopy->sub(new DateInterval('PT' . Format::secondsByMonth($dateCopy) . 'S')); } return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime); case '1y': $dateCopy = clone $dateTime; for ($i = 0; $i < 4; $i++) { $dateCopy->sub(new DateInterval('PT' . Format::secondsByYear($dateCopy) . 'S')); } return $dateCopy->add(new DateInterval('PT1S'))->diff($dateTime); default: return DateInterval::createFromDateString('1 day -1 second'); } }
/** * Apply the current interval to the given date and time * * @param DateTime $dateTime The date and time to apply the interval to * @param int $adjustBy By how much seconds the resulting date and time should be adjusted * * @return DateTime */ protected function applyInterval(DateTime $dateTime, $adjustBy) { if (!$this->interval->y && !$this->interval->m) { if ($this->negative) { return $dateTime->sub($this->interval)->add(new DateInterval('PT' . $adjustBy . 'S')); } else { return $dateTime->add($this->interval)->sub(new DateInterval('PT' . $adjustBy . 'S')); } } elseif ($this->interval->m) { for ($i = 0; $i < $this->interval->m; $i++) { if ($this->negative) { $dateTime->sub(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S')); } else { $dateTime->add(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S')); } } } elseif ($this->interval->y) { for ($i = 0; $i < $this->interval->y; $i++) { if ($this->negative) { $dateTime->sub(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S')); } else { $dateTime->add(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S')); } } } $adjustment = new DateInterval('PT' . $adjustBy . 'S'); return $this->negative ? $dateTime->add($adjustment) : $dateTime->sub($adjustment); }