|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the DateTime library. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Artem Henvald <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace Fresh\DateTime; |
|
14
|
|
|
|
|
15
|
|
|
use Fresh\DateTime\Exception\UnexpectedValueException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* DateTimeHelper. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Artem Henvald <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class DateTimeHelper |
|
23
|
|
|
{ |
|
24
|
|
|
private const INTERNAL_DATE_FORMAT = 'Y-m-d'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \DateTimeZone|null $timeZone |
|
28
|
|
|
* |
|
29
|
|
|
* @return \DateTime |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getCurrentDatetime(?\DateTimeZone $timeZone = null): \DateTime |
|
32
|
|
|
{ |
|
33
|
|
|
return new \DateTime('now', $timeZone); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \DateTimeZone|null $timeZone |
|
38
|
|
|
* |
|
39
|
|
|
* @return \DateTimeImmutable |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getCurrentDatetimeImmutable(?\DateTimeZone $timeZone = null): \DateTimeImmutable |
|
42
|
|
|
{ |
|
43
|
|
|
return new \DateTimeImmutable('now', $timeZone); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param DateRange $dateRange |
|
48
|
|
|
* |
|
49
|
|
|
* @throws UnexpectedValueException |
|
50
|
|
|
* |
|
51
|
|
|
* @return \DateTimeInterface[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getDatesFromDateRangeAsArrayOfObjects(DateRange $dateRange): array |
|
54
|
|
|
{ |
|
55
|
|
|
$dateRange->assertSameTimezones(); |
|
56
|
|
|
|
|
57
|
|
|
$since = $this->cloneDateTime($dateRange->getSince()); |
|
58
|
|
|
$since->setTime(0, 0); |
|
59
|
|
|
|
|
60
|
|
|
$till = $this->cloneDateTime($dateRange->getTill()); |
|
61
|
|
|
$till->setTime(23, 59, 59); |
|
62
|
|
|
|
|
63
|
|
|
$datesAsObjects = []; |
|
64
|
|
|
$period = new \DatePeriod($since, new \DateInterval('P1D'), $till); |
|
65
|
|
|
foreach ($period as $date) { |
|
66
|
|
|
$datesAsObjects[] = $date; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $datesAsObjects; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param DateRange $dateRange |
|
74
|
|
|
* |
|
75
|
|
|
* @return string[] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getDatesFromDateRangeAsArrayOfStrings(DateRange $dateRange): array |
|
78
|
|
|
{ |
|
79
|
|
|
$datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange); |
|
80
|
|
|
|
|
81
|
|
|
$datesAsStrings = []; |
|
82
|
|
|
foreach ($datesAsObjects as $datesAsObject) { |
|
83
|
|
|
$datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $datesAsStrings; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param \DateTimeInterface $originalDate |
|
91
|
|
|
* |
|
92
|
|
|
* @throws UnexpectedValueException |
|
93
|
|
|
* |
|
94
|
|
|
* @return \DateTime |
|
95
|
|
|
*/ |
|
96
|
|
|
private function cloneDateTime(\DateTimeInterface $originalDate): \DateTime |
|
97
|
|
|
{ |
|
98
|
|
|
$date = \DateTime::createFromFormat(\DateTime::RFC3339, $originalDate->format(\DateTime::RFC3339), $originalDate->getTimezone()); |
|
99
|
|
|
|
|
100
|
|
|
if (!$date instanceof \DateTime) { |
|
101
|
|
|
throw new UnexpectedValueException(\sprintf('Could not create %s object', \DateTime::class)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $date; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|