Campaigns.php
3 years ago
City_Statistics.php
3 years ago
Country_Statistics.php
3 years ago
Current_Traffic_Finder.php
3 years ago
Interval.php
3 years ago
Minute_Interval.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
Ten_Second_Interval.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
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Queries; |
| 4 | |
| 5 | use IAWP\Utils\Timezone; |
| 6 | |
| 7 | abstract class Range_Query |
| 8 | { |
| 9 | private $start; |
| 10 | private $end; |
| 11 | const FORMAT_PATTERN = 'Y-m-d H:i:s'; |
| 12 | |
| 13 | public function __construct($options = []) |
| 14 | { |
| 15 | $options = iawp_default_args($options, [ |
| 16 | 'start' => new \DateTime('1991-01-06'), |
| 17 | 'end' => new \DateTime(), |
| 18 | 'convert_to_full_days' => true, |
| 19 | ]); |
| 20 | |
| 21 | $this->start = clone $options['start']; |
| 22 | $this->end = clone $options['end']; |
| 23 | |
| 24 | if ($options['convert_to_full_days']) { |
| 25 | $this->start = $this->start_of_locale_day($this->start); |
| 26 | $this->end = $this->end_of_locale_day($this->end); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | private function start_of_locale_day(\DateTime $datetime): \DateTime |
| 31 | { |
| 32 | $user_timezone = new \DateTimeZone(Timezone::local_offset()); |
| 33 | $utc_timezone = new \DateTimeZone('UTC'); |
| 34 | |
| 35 | return $datetime->setTimezone($user_timezone)->setTime(0, 0, 0)->setTimezone($utc_timezone); |
| 36 | } |
| 37 | |
| 38 | private function end_of_locale_day(\DateTime $datetime): \DateTime |
| 39 | { |
| 40 | $user_timezone = new \DateTimeZone(Timezone::local_offset()); |
| 41 | $utc_timezone = new \DateTimeZone('UTC'); |
| 42 | |
| 43 | return $datetime->setTimezone($user_timezone)->setTime(23, 59, 59)->setTimezone($utc_timezone); |
| 44 | } |
| 45 | |
| 46 | final public function start() |
| 47 | { |
| 48 | return $this->start; |
| 49 | } |
| 50 | |
| 51 | final protected function formatted_start() |
| 52 | { |
| 53 | return $this->start->format(self::FORMAT_PATTERN); |
| 54 | } |
| 55 | |
| 56 | final public function end() |
| 57 | { |
| 58 | return $this->end; |
| 59 | } |
| 60 | |
| 61 | final protected function formatted_end() |
| 62 | { |
| 63 | return $this->end->format(self::FORMAT_PATTERN); |
| 64 | } |
| 65 | |
| 66 | private function range_size() |
| 67 | { |
| 68 | return $this->start->diff($this->end)->days + 1; |
| 69 | } |
| 70 | |
| 71 | final public function prev_period_start() |
| 72 | { |
| 73 | $prev_start = clone $this->start; |
| 74 | $range_size = $this->range_size(); |
| 75 | |
| 76 | return $prev_start->modify("-$range_size days"); |
| 77 | } |
| 78 | |
| 79 | final protected function prev_period_formatted_start() |
| 80 | { |
| 81 | return $this->prev_period_start()->format(self::FORMAT_PATTERN); |
| 82 | } |
| 83 | |
| 84 | final public function prev_period_end() |
| 85 | { |
| 86 | $prev_end = clone $this->end; |
| 87 | $range_size = $this->range_size(); |
| 88 | |
| 89 | return $prev_end->modify("-$range_size days"); |
| 90 | } |
| 91 | |
| 92 | final protected function prev_period_formatted_end() |
| 93 | { |
| 94 | return $this->prev_period_end()->format(self::FORMAT_PATTERN); |
| 95 | } |
| 96 | } |
| 97 |