Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
DateTime
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
10 / 10
16
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 getWeekOfMonth
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 isWeekOfMonth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLastWeekOfMonth
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSecondsOfDay
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getFormatedDates
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getSummerTimeStartDateTime
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getSummerTimeEndDateTime
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace BO\Zmsentities\Helper;
4
5class 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, $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 = $this->format('j');
38        $weekOfMonth = ceil($dayOfMonth / 7);
39        return $weekOfMonth;
40    }
41
42    public function isWeekOfMonth($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 = $this->format('G');
57        $minutes = $this->format('i');
58        $seconds = $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        $locale = 'de_DE',
70        $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($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('02', '00', '00');
89    }
90
91    public static function getSummerTimeEndDateTime($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('03', '00', '00');
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}