Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function getIntervals()
- {
- $intervals = array();
- $formatHigh = 'Y-m-d 23:59:59';
- switch ($this->step) {
- case self::STEP_DAYS:
- $formatLow = 'Y-m-d 00:00:00';
- $lowestDate = date($formatLow, $this->from);
- $highestDate = date($formatHigh, $this->to);
- $currentDate = date($formatLow, $this->from);
- while ($currentDate < $highestDate) {
- $low = $currentDate;
- $high = date($formatHigh, strtotime($currentDate));
- $currentDate = date($formatLow, strtotime('+1 day', strtotime($currentDate)));
- $intervals[] = compact('low', 'high');
- }
- break;
- case self::STEP_WEEKS:
- $formatLow = 'Y-m-d 00:00:00';
- $lowestDate = date($formatLow, $this->from);
- $highestDate = date($formatHigh, $this->to);
- if ($this->step_mode == self::STEP_MODE_STANDARD) {
- $year = date('Y', $this->from);
- $week = date('W', $this->from);
- $currentDate = date($formatLow, strtotime(sprintf('%s-W%s', $year, $week)));
- while ($currentDate < $highestDate) {
- $low = $currentDate;
- $currentDate = date($formatLow, strtotime('+1 week', strtotime($currentDate)));
- $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
- $intervals[] = compact('low', 'high');
- }
- } else { //self::STEP_MODE_INTERVAL
- $currentDate = date($formatLow, $this->from);
- while ($currentDate < $highestDate) {
- $low = $currentDate;
- $currentDate = date($formatLow, strtotime('+7 days', strtotime($currentDate)));
- $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
- $intervals[] = compact('low', 'high');
- }
- }
- break;
- case self::STEP_MONTHS:
- if ($this->step_mode == self::STEP_MODE_STANDARD) {
- $formatLow = 'Y-m-01 00:00:00';
- $lowestDate = date($formatLow, $this->from);
- $currentDate = $lowestDate;
- $highestDate = date($formatHigh, strtotime(sprintf(
- 'last day of %s %d',
- date('F', $this->to),
- date('Y', $this->to)
- )));
- while ($currentDate < $highestDate) {
- $tsCurrentDate = strtotime($currentDate);
- $low = $currentDate;
- $high = date($formatHigh, strtotime(sprintf(
- 'last day of %s %d -1 day',
- date('F', $tsCurrentDate),
- date('Y', $tsCurrentDate)
- )));
- $intervals[] = compact('low', 'high');
- $currentDate = date($formatLow, strtotime('+1 month', strtotime($currentDate)));
- }
- } else { //self::STEP_MODE_INTERVAL
- $formatLow = 'Y-m-d 00:00:00';
- $currentDate = date($formatLow, $this->from);
- $highestDate = date($formatHigh, $this->to);
- while ($currentDate < $highestDate) {
- $low = $currentDate;
- $currentDate = date($formatLow, strtotime('+30 days', strtotime($currentDate)));
- $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
- $intervals[] = compact('low', 'high');
- }
- }
- break;
- case self::STEP_YEARS:
- if ($this->step_mode == self::STEP_MODE_STANDARD) {
- $formatLow = 'Y-01-01 00:00:00';
- $lowestDate = date($formatLow, $this->from);
- $currentDate = $lowestDate;
- $highestDate = date($formatHigh, $this->to);
- while ($currentDate < $highestDate) {
- $low = $currentDate;
- $currentDate = date($formatLow, strtotime('+1 year', strtotime($currentDate)));
- $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
- $intervals[] = compact('low', 'high');
- }
- } else { //self::STEP_MODE_INTERVAL
- $formatLow = 'Y-m-d 00:00:00';
- $lowestDate = date($formatLow, $this->from);
- $currentDate = $lowestDate;
- $highestDate = date($formatHigh, $this->to);
- while ($currentDate < $highestDate) {
- $low = $currentDate;
- $currentDate = date($formatLow, strtotime('+365 days', strtotime($currentDate)));
- $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
- $intervals[] = compact('low', 'high');
- }
- }
- break;
- }
- if (empty($intervals) === false) {
- if ($intervals[0]['low'] < $lowestDate) {
- $intervals[0]['low'] = $lowestDate;
- }
- end($intervals);
- $lastIntervalsKey = key($intervals);
- if ($intervals[$lastIntervalsKey]['high'] > $highestDate) {
- $intervals[$lastIntervalsKey]['high'] = $highestDate;
- }
- reset($intervals);
- }
- return $intervals;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement