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 \DateTimeImmutable[] |
52
|
|
|
*/ |
53
|
|
|
public function getDatesFromDateRangeAsArrayOfObjects(DateRange $dateRange): array |
54
|
|
|
{ |
55
|
|
|
$datesAsObjects = []; |
56
|
|
|
|
57
|
|
|
if ($dateRange->getSince()->format(self::INTERNAL_DATE_FORMAT) !== $dateRange->getTill()->format(self::INTERNAL_DATE_FORMAT)) { |
58
|
|
|
$till = \DateTime::createFromFormat( |
59
|
|
|
'U', |
60
|
|
|
(string) $dateRange->getTill()->getTimestamp(), |
61
|
|
|
$dateRange->getTill()->getTimezone() |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
if (!$till instanceof \DateTime) { |
65
|
|
|
throw new UnexpectedValueException(\sprintf('Could not create %s object', \DateTime::class)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$period = new \DatePeriod($dateRange->getSince(), new \DateInterval('P1D'), $till); |
69
|
|
|
|
70
|
|
|
/** @var \DateTime $date */ |
71
|
|
|
foreach ($period as $date) { |
72
|
|
|
$datesAsObjects[] = $date; |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
// If since and till dates are equal, then only one date in range |
76
|
|
|
$datesAsObjects[] = $dateRange->getSince(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return \array_values($datesAsObjects); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param DateRange $dateRange |
84
|
|
|
* |
85
|
|
|
* @return string[] |
86
|
|
|
*/ |
87
|
|
|
public function getDatesFromDateRangeAsArrayOfStrings(DateRange $dateRange): array |
88
|
|
|
{ |
89
|
|
|
$datesAsObjects = $this->getDatesFromDateRangeAsArrayOfObjects($dateRange); |
90
|
|
|
|
91
|
|
|
$datesAsStrings = []; |
92
|
|
|
foreach ($datesAsObjects as $datesAsObject) { |
93
|
|
|
$datesAsStrings[] = $datesAsObject->format(self::INTERNAL_DATE_FORMAT); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $datesAsStrings; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|