Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
42 / 42 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
DateTime | |
100.00% |
42 / 42 |
|
100.00% |
10 / 10 |
16 | |
100.00% |
1 / 1 |
create | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
getWeekOfMonth | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isWeekOfMonth | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isLastWeekOfMonth | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getSecondsOfDay | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getFormatedDates | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
getSummerTimeStartDateTime | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getSummerTimeEndDateTime | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities\Helper; |
4 | |
5 | class DateTime extends \DateTimeImmutable implements \JsonSerializable |
6 | { |
7 | public static function create($time = 'now', \DateTimeZone $timezone = null) |
8 | { |
9 | if ($time instanceof \BO\Zmsentities\Helper\DateTime) { |
10 | $dateTime = $time; |
11 | if (null !== $timezone) { |
12 | $dateTime = $dateTime->setTimezone($timezone); |
13 | } |
14 | } elseif ($time instanceof \DateTimeInterface) { |
15 | $dateTime = new self(); |
16 | if (null !== $timezone) { |
17 | $dateTime = $dateTime->setTimezone($timezone); |
18 | } else { |
19 | $dateTime = $dateTime->setTimezone($time->getTimezone()); |
20 | } |
21 | $dateTime = $dateTime->setTimestamp($time->getTimestamp()); |
22 | } else { |
23 | $dateTime = new self($time, $timezone); |
24 | } |
25 | return $dateTime; |
26 | } |
27 | |
28 | public function getWeekOfMonth() |
29 | { |
30 | // Todo: This is correct way of calculating week of month by date, but zms1 has 1-7 = 1, 8-14 = 2,... |
31 | /* |
32 | $week = $this->format('W'); |
33 | $firstWeekOfMonth = $this->modify('first day of this month')->format('W'); |
34 | return 1 + ($week < $firstWeekOfMonth ? $week : $week - $firstWeekOfMonth); |
35 | */ |
36 | |
37 | $dayOfMonth = $this->format('j'); |
38 | $weekOfMonth = ceil($dayOfMonth / 7); |
39 | return $weekOfMonth; |
40 | } |
41 | |
42 | public function isWeekOfMonth($number) |
43 | { |
44 | return (int)$this->getWeekOfMonth() === (int)$number; |
45 | } |
46 | |
47 | public function isLastWeekOfMonth() |
48 | { |
49 | $weekOfMonth = $this->getWeekOfMonth(); |
50 | $lastDay = $this->modify('last day of this month'); |
51 | return $weekOfMonth == $lastDay->getWeekOfMonth(); |
52 | } |
53 | |
54 | public function getSecondsOfDay() |
55 | { |
56 | $hours = $this->format('G'); |
57 | $minutes = $this->format('i'); |
58 | $seconds = $this->format('s'); |
59 | return $hours * 3600 + $minutes * 60 + $seconds; |
60 | } |
61 | |
62 | public static function getFormatedDates( |
63 | \DateTimeInterface $date, |
64 | $pattern = 'MMMM', |
65 | $locale = 'de_DE', |
66 | $timezone = 'Europe/Berlin' |
67 | ) { |
68 | $dateFormatter = new \IntlDateFormatter( |
69 | $locale, |
70 | \IntlDateFormatter::MEDIUM, |
71 | \IntlDateFormatter::MEDIUM, |
72 | $timezone, |
73 | \IntlDateFormatter::GREGORIAN, |
74 | $pattern |
75 | ); |
76 | return $dateFormatter->format($date->getTimestamp()); |
77 | } |
78 | |
79 | public static function getSummerTimeStartDateTime($year = null) |
80 | { |
81 | $year = ($year) ? $year : date('Y'); |
82 | $dateTimeMarch = new \DateTime($year . '-03-01', new \DateTimeZone('Europe/Berlin')); |
83 | $lastSunday = $dateTimeMarch->modify('Last Sunday of March'); |
84 | return $lastSunday->setTime('02', '00', '00'); |
85 | } |
86 | |
87 | public static function getSummerTimeEndDateTime($year = null) |
88 | { |
89 | $year = ($year) ? $year : date('Y'); |
90 | $dateTimeOctober = new \DateTime($year . '-10-01', new \DateTimeZone('Europe/Berlin')); |
91 | $lastSunday = $dateTimeOctober->modify('Last Sunday of October'); |
92 | return $lastSunday->setTime('03', '00', '00'); |
93 | } |
94 | |
95 | public function __toString(): string |
96 | { |
97 | return $this->format(DATE_ATOM); |
98 | } |
99 | |
100 | public function jsonSerialize(): string |
101 | { |
102 | return $this->format(DATE_ATOM); |
103 | } |
104 | } |