range-query.php
4 years ago
referrers.php
4 years ago
resources.php
4 years ago
views.php
4 years ago
range-query.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | if (!class_exists('IAWP_Range_Query')) { |
| 4 | abstract class IAWP_Range_Query |
| 5 | { |
| 6 | private $start; |
| 7 | private $end; |
| 8 | const FORMAT_PATTERN = 'Y-m-d H:i:s'; |
| 9 | |
| 10 | public function __construct($start = null, $end = null) |
| 11 | { |
| 12 | if (is_null($start) || is_null($end)) { |
| 13 | list($start, $end) = IAWP()->default_date_range(); |
| 14 | } |
| 15 | |
| 16 | $this->start = (clone $start)->setTime(0, 0, 0); |
| 17 | $this->end = (clone $end)->setTime(23, 59, 59); |
| 18 | } |
| 19 | |
| 20 | final public function start() |
| 21 | { |
| 22 | return $this->start; |
| 23 | } |
| 24 | |
| 25 | final protected function formatted_start() |
| 26 | { |
| 27 | return $this->start->format(self::FORMAT_PATTERN); |
| 28 | } |
| 29 | |
| 30 | final public function end() |
| 31 | { |
| 32 | return $this->end; |
| 33 | } |
| 34 | |
| 35 | final protected function formatted_end() |
| 36 | { |
| 37 | return $this->end->format(self::FORMAT_PATTERN); |
| 38 | } |
| 39 | |
| 40 | private function range_size() |
| 41 | { |
| 42 | return $this->start->diff($this->end)->days + 1; |
| 43 | } |
| 44 | |
| 45 | final public function prev_period_start() |
| 46 | { |
| 47 | $prev_start = clone $this->start; |
| 48 | $range_size = $this->range_size(); |
| 49 | |
| 50 | return $prev_start->modify("-$range_size days"); |
| 51 | } |
| 52 | |
| 53 | final protected function prev_period_formatted_start() |
| 54 | { |
| 55 | return $this->prev_period_start()->format(self::FORMAT_PATTERN); |
| 56 | } |
| 57 | |
| 58 | final public function prev_period_end() |
| 59 | { |
| 60 | $prev_end = clone $this->end; |
| 61 | $range_size = $this->range_size(); |
| 62 | |
| 63 | return $prev_end->modify("-$range_size days"); |
| 64 | } |
| 65 | |
| 66 | final protected function prev_period_formatted_end() |
| 67 | { |
| 68 | return $this->prev_period_end()->format(self::FORMAT_PATTERN); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 |