campaigns.php
3 years ago
current_traffic_finder.php
3 years ago
geos.php
3 years ago
range_query.php
3 years ago
referrers.php
3 years ago
reset_database.php
3 years ago
resources.php
3 years ago
view.php
3 years ago
views.php
3 years ago
visitors_over_time_finder.php
3 years ago
range_query.php
95 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($options = []) |
| 12 | { |
| 13 | $options = default_args($options, [ |
| 14 | 'start' => new \DateTime('1991-01-06'), |
| 15 | 'end' => new \DateTime(), |
| 16 | 'convert_to_full_days' => true, |
| 17 | ]); |
| 18 | |
| 19 | $this->start = clone $options['start']; |
| 20 | $this->end = clone $options['end']; |
| 21 | |
| 22 | if ($options['convert_to_full_days']) { |
| 23 | $this->start = $this->start_of_locale_day($this->start); |
| 24 | $this->end = $this->end_of_locale_day($this->end); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | private function start_of_locale_day(\DateTime $datetime): \DateTime |
| 29 | { |
| 30 | $user_timezone = new \DateTimeZone(Timezone::local_offset()); |
| 31 | $utc_timezone = new \DateTimeZone('UTC'); |
| 32 | |
| 33 | return $datetime->setTimezone($user_timezone)->setTime(0, 0, 0)->setTimezone($utc_timezone); |
| 34 | } |
| 35 | |
| 36 | private function end_of_locale_day(\DateTime $datetime): \DateTime |
| 37 | { |
| 38 | $user_timezone = new \DateTimeZone(Timezone::local_offset()); |
| 39 | $utc_timezone = new \DateTimeZone('UTC'); |
| 40 | |
| 41 | return $datetime->setTimezone($user_timezone)->setTime(23, 59, 59)->setTimezone($utc_timezone); |
| 42 | } |
| 43 | |
| 44 | final public function start() |
| 45 | { |
| 46 | return $this->start; |
| 47 | } |
| 48 | |
| 49 | final protected function formatted_start() |
| 50 | { |
| 51 | return $this->start->format(self::FORMAT_PATTERN); |
| 52 | } |
| 53 | |
| 54 | final public function end() |
| 55 | { |
| 56 | return $this->end; |
| 57 | } |
| 58 | |
| 59 | final protected function formatted_end() |
| 60 | { |
| 61 | return $this->end->format(self::FORMAT_PATTERN); |
| 62 | } |
| 63 | |
| 64 | private function range_size() |
| 65 | { |
| 66 | return $this->start->diff($this->end)->days + 1; |
| 67 | } |
| 68 | |
| 69 | final public function prev_period_start() |
| 70 | { |
| 71 | $prev_start = clone $this->start; |
| 72 | $range_size = $this->range_size(); |
| 73 | |
| 74 | return $prev_start->modify("-$range_size days"); |
| 75 | } |
| 76 | |
| 77 | final protected function prev_period_formatted_start() |
| 78 | { |
| 79 | return $this->prev_period_start()->format(self::FORMAT_PATTERN); |
| 80 | } |
| 81 | |
| 82 | final public function prev_period_end() |
| 83 | { |
| 84 | $prev_end = clone $this->end; |
| 85 | $range_size = $this->range_size(); |
| 86 | |
| 87 | return $prev_end->modify("-$range_size days"); |
| 88 | } |
| 89 | |
| 90 | final protected function prev_period_formatted_end() |
| 91 | { |
| 92 | return $this->prev_period_end()->format(self::FORMAT_PATTERN); |
| 93 | } |
| 94 | } |
| 95 |