Lines
100.00%
42 / 42
Methods
100.00%
10 / 10
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| create | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 6 | ||
| 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 | ||
| 5 | class DateTime extends \DateTimeImmutable implements \JsonSerializable | |
| 6 | { | |
| 7 | public static function create(string|\DateTimeInterface|false $time = 'now', ?\DateTimeZone $timezone = null): self | |
| 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 === false ? 'now' : $time, $timezone); | |
| 24 | } | |
| 25 | return $dateTime; | |
| 26 | } | |
| 27 | ||
| 28 | public function getWeekOfMonth(): float | |
| 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 = (int) $this->format('j'); | |
| 38 | $weekOfMonth = ceil($dayOfMonth / 7); | |
| 39 | return $weekOfMonth; | |
| 40 | } | |
| 41 | ||
| 42 | public function isWeekOfMonth(mixed $number): bool | |
| 43 | { | |
| 44 | return (int)$this->getWeekOfMonth() === (int)$number; | |
| 45 | } | |
| 46 | ||
| 47 | public function isLastWeekOfMonth(): bool | |
| 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(): int|float | |
| 55 | { | |
| 56 | $hours = (int) $this->format('G'); | |
| 57 | $minutes = (int) $this->format('i'); | |
| 58 | $seconds = (int) $this->format('s'); | |
| 59 | return $hours * 3600 + $minutes * 60 + $seconds; | |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * | |
| 64 | * @return false|string | |
| 65 | */ | |
| 66 | public static function getFormatedDates( | |
| 67 | \DateTimeInterface $date, | |
| 68 | string $pattern = 'MMMM', | |
| 69 | string $locale = 'de_DE', | |
| 70 | string $timezone = 'Europe/Berlin' | |
| 71 | ): string|false { | |
| 72 | $dateFormatter = new \IntlDateFormatter( | |
| 73 | $locale, | |
| 74 | \IntlDateFormatter::MEDIUM, | |
| 75 | \IntlDateFormatter::MEDIUM, | |
| 76 | $timezone, | |
| 77 | \IntlDateFormatter::GREGORIAN, | |
| 78 | $pattern | |
| 79 | ); | |
| 80 | return $dateFormatter->format($date->getTimestamp()); | |
| 81 | } | |
| 82 | ||
| 83 | public static function getSummerTimeStartDateTime(mixed $year = null): \DateTime | |
| 84 | { | |
| 85 | $year = ($year) ? $year : date('Y'); | |
| 86 | $dateTimeMarch = new \DateTime($year . '-03-01', new \DateTimeZone('Europe/Berlin')); | |
| 87 | $lastSunday = $dateTimeMarch->modify('Last Sunday of March'); | |
| 88 | return $lastSunday->setTime(2, 0, 0); | |
| 89 | } | |
| 90 | ||
| 91 | public static function getSummerTimeEndDateTime(mixed $year = null): \DateTime | |
| 92 | { | |
| 93 | $year = ($year) ? $year : date('Y'); | |
| 94 | $dateTimeOctober = new \DateTime($year . '-10-01', new \DateTimeZone('Europe/Berlin')); | |
| 95 | $lastSunday = $dateTimeOctober->modify('Last Sunday of October'); | |
| 96 | return $lastSunday->setTime(3, 0, 0); | |
| 97 | } | |
| 98 | ||
| 99 | public function __toString(): string | |
| 100 | { | |
| 101 | return $this->format(DATE_ATOM); | |
| 102 | } | |
| 103 | ||
| 104 | #[\Override] | |
| 105 | public function jsonSerialize(): string | |
| 106 | { | |
| 107 | return $this->format(DATE_ATOM); | |
| 108 | } | |
| 109 | } |