CustomFormats
5 years ago
Locales
5 years ago
FormatsInterface.php
5 years ago
LICENSE.md
5 years ago
Moment.php
5 years ago
MomentException.php
5 years ago
MomentFromVo.php
5 years ago
MomentHelper.php
5 years ago
MomentLocale.php
5 years ago
MomentPeriodVo.php
5 years ago
MomentHelper.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPressPopularPosts\Moment; |
| 4 | |
| 5 | /** |
| 6 | * MomentHelper |
| 7 | * @package Moment |
| 8 | * @author Tino Ehrich (tino@bigpun.me) |
| 9 | */ |
| 10 | class MomentHelper |
| 11 | { |
| 12 | /** |
| 13 | * @param $quarter |
| 14 | * @param $year |
| 15 | * @param string $timeZoneString |
| 16 | * |
| 17 | * @return MomentPeriodVo |
| 18 | * @throws MomentException |
| 19 | */ |
| 20 | public static function getQuarterPeriod($quarter, $year, $timeZoneString = 'UTC') |
| 21 | { |
| 22 | switch ($quarter) |
| 23 | { |
| 24 | case 1: |
| 25 | $startMonth = 1; |
| 26 | $endMonth = 3; |
| 27 | break; |
| 28 | case 2: |
| 29 | $startMonth = 4; |
| 30 | $endMonth = 6; |
| 31 | break; |
| 32 | case 3: |
| 33 | $startMonth = 7; |
| 34 | $endMonth = 9; |
| 35 | break; |
| 36 | case 4: |
| 37 | $startMonth = 10; |
| 38 | $endMonth = 12; |
| 39 | break; |
| 40 | default: |
| 41 | throw new MomentException('Invalid quarter. The range of quarters is 1 - 4. You asked for: ' . $quarter); |
| 42 | } |
| 43 | |
| 44 | // set start |
| 45 | $start = new Moment(); |
| 46 | $start |
| 47 | ->setTimezone($timeZoneString) |
| 48 | ->setYear($year) |
| 49 | ->setMonth($startMonth) |
| 50 | ->setDay(1) |
| 51 | ->setTime(0, 0, 0); |
| 52 | |
| 53 | // set end |
| 54 | $end = new Moment(); |
| 55 | $end |
| 56 | ->setTimezone($timeZoneString) |
| 57 | ->setYear($year) |
| 58 | ->setMonth($endMonth) |
| 59 | ->setDay($end->format('t')) |
| 60 | ->setTime(23, 59, 59); |
| 61 | |
| 62 | // set period vo |
| 63 | $momentPeriodVo = new MomentPeriodVo(); |
| 64 | |
| 65 | return $momentPeriodVo |
| 66 | ->setInterval($quarter) |
| 67 | ->setStartDate($start) |
| 68 | ->setEndDate($end); |
| 69 | } |
| 70 | } |