Advertisement
Guest User

Untitled

a guest
May 25th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.43 KB | None | 0 0
  1. public function getIntervals()
  2. {
  3.     $intervals = array();
  4.     $formatHigh = 'Y-m-d 23:59:59';
  5.     switch ($this->step) {
  6.         case self::STEP_DAYS:
  7.             $formatLow = 'Y-m-d 00:00:00';
  8.             $lowestDate = date($formatLow, $this->from);
  9.             $highestDate = date($formatHigh, $this->to);
  10.             $currentDate = date($formatLow, $this->from);
  11.             while ($currentDate < $highestDate) {
  12.                 $low = $currentDate;
  13.                 $high = date($formatHigh, strtotime($currentDate));
  14.                 $currentDate = date($formatLow, strtotime('+1 day', strtotime($currentDate)));
  15.                 $intervals[] = compact('low', 'high');
  16.             }
  17.             break;
  18.         case self::STEP_WEEKS:
  19.             $formatLow = 'Y-m-d 00:00:00';
  20.             $lowestDate = date($formatLow, $this->from);
  21.             $highestDate = date($formatHigh, $this->to);
  22.             if ($this->step_mode == self::STEP_MODE_STANDARD) {
  23.                 $year = date('Y', $this->from);
  24.                 $week = date('W', $this->from);
  25.                 $currentDate = date($formatLow, strtotime(sprintf('%s-W%s', $year, $week)));
  26.                 while ($currentDate < $highestDate) {
  27.                     $low = $currentDate;
  28.                     $currentDate = date($formatLow, strtotime('+1 week', strtotime($currentDate)));
  29.                     $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
  30.                     $intervals[] = compact('low', 'high');
  31.                 }
  32.             } else { //self::STEP_MODE_INTERVAL
  33.                 $currentDate = date($formatLow, $this->from);
  34.                 while ($currentDate < $highestDate) {
  35.                     $low = $currentDate;
  36.                     $currentDate = date($formatLow, strtotime('+7 days', strtotime($currentDate)));
  37.                     $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
  38.                     $intervals[] = compact('low', 'high');
  39.                 }
  40.             }
  41.             break;
  42.         case self::STEP_MONTHS:
  43.             if ($this->step_mode == self::STEP_MODE_STANDARD) {
  44.                 $formatLow = 'Y-m-01 00:00:00';
  45.                 $lowestDate = date($formatLow, $this->from);
  46.                 $currentDate = $lowestDate;
  47.                 $highestDate = date($formatHigh, strtotime(sprintf(
  48.                     'last day of %s %d',
  49.                     date('F', $this->to),
  50.                     date('Y', $this->to)
  51.                 )));
  52.                 while ($currentDate < $highestDate) {
  53.                     $tsCurrentDate = strtotime($currentDate);
  54.                     $low = $currentDate;
  55.                     $high = date($formatHigh, strtotime(sprintf(
  56.                         'last day of %s %d -1 day',
  57.                         date('F', $tsCurrentDate),
  58.                         date('Y', $tsCurrentDate)
  59.                     )));
  60.                     $intervals[] = compact('low', 'high');
  61.                     $currentDate = date($formatLow, strtotime('+1 month', strtotime($currentDate)));
  62.                 }
  63.             } else { //self::STEP_MODE_INTERVAL
  64.                 $formatLow = 'Y-m-d 00:00:00';
  65.                 $currentDate = date($formatLow, $this->from);
  66.                 $highestDate = date($formatHigh, $this->to);
  67.                 while ($currentDate < $highestDate) {
  68.                     $low = $currentDate;
  69.                     $currentDate = date($formatLow, strtotime('+30 days', strtotime($currentDate)));
  70.                     $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
  71.                     $intervals[] = compact('low', 'high');
  72.                 }
  73.             }
  74.             break;
  75.         case self::STEP_YEARS:
  76.             if ($this->step_mode == self::STEP_MODE_STANDARD) {
  77.                 $formatLow = 'Y-01-01 00:00:00';
  78.                 $lowestDate = date($formatLow, $this->from);
  79.                 $currentDate = $lowestDate;
  80.                 $highestDate = date($formatHigh, $this->to);
  81.                 while ($currentDate < $highestDate) {
  82.                     $low = $currentDate;
  83.                     $currentDate = date($formatLow, strtotime('+1 year', strtotime($currentDate)));
  84.                     $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
  85.                     $intervals[] = compact('low', 'high');
  86.                 }
  87.             } else { //self::STEP_MODE_INTERVAL
  88.                 $formatLow = 'Y-m-d 00:00:00';
  89.                 $lowestDate = date($formatLow, $this->from);
  90.                 $currentDate = $lowestDate;
  91.                 $highestDate = date($formatHigh, $this->to);
  92.                 while ($currentDate < $highestDate) {
  93.                     $low = $currentDate;
  94.                     $currentDate = date($formatLow, strtotime('+365 days', strtotime($currentDate)));
  95.                     $high = date($formatHigh, strtotime(sprintf('%s - 1 day', $currentDate)));
  96.                     $intervals[] = compact('low', 'high');
  97.                 }
  98.             }
  99.             break;
  100.     }
  101.     if (empty($intervals) === false) {
  102.         if ($intervals[0]['low'] < $lowestDate) {
  103.             $intervals[0]['low'] = $lowestDate;
  104.         }
  105.         end($intervals);
  106.         $lastIntervalsKey = key($intervals);
  107.         if ($intervals[$lastIntervalsKey]['high'] > $highestDate) {
  108.             $intervals[$lastIntervalsKey]['high'] = $highestDate;
  109.         }
  110.         reset($intervals);
  111.     }
  112.     return $intervals;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement