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