1
|
|
|
<?php |
2
|
|
|
namespace Brtriver\DateRange; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use DateInterval; |
6
|
|
|
use DatePeriod; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
|
9
|
|
|
class DateRange implements IteratorAggregate |
10
|
|
|
{ |
11
|
|
|
private $start; |
12
|
|
|
private $end; |
13
|
|
|
private $interval; |
14
|
|
|
private $excludeStartDate = false; |
15
|
|
|
private $excludeEndDate = false; |
16
|
|
|
const INTERVAL = 'P1D'; |
17
|
|
|
|
18
|
|
|
public function __construct() { |
19
|
|
|
$this->parseArguments(func_get_args()); |
20
|
|
|
|
21
|
|
|
if (!($this->start instanceof DateTime) || !($this->end instanceof DateTime)) { |
22
|
|
|
throw new \InvalidArgumentException('cannot parse start and end date'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if ($this->start->getTimestamp() > $this->end->getTimestamp()) { |
26
|
|
|
throw new \InvalidArgumentException('end date is the day before than start date'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->interval = new DateInterval(self::INTERVAL); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private function parseArguments($args) |
33
|
|
|
{ |
34
|
|
|
if (count($args) === 1) { |
35
|
|
|
list($this->start, $this->end) = self::getDateFromArray($args[0]); |
36
|
|
|
} elseif (count($args) === 2) { |
37
|
|
|
$this->start = self::convertToDateTime($args[0]); |
38
|
|
|
$this->end = self::convertToDateTime($args[1]); |
39
|
|
|
} else { |
40
|
|
|
throw new \InvalidArgumentException('Invalid number of arguments'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private static function getDateFromArray($startEndArray) |
45
|
|
|
{ |
46
|
|
|
$start = $end = null; |
47
|
|
|
|
48
|
|
|
if (is_array($startEndArray) && count($startEndArray) === 2) { |
49
|
|
|
$values = array_values($startEndArray); |
50
|
|
|
$start = self::convertToDateTime($values[0]); |
51
|
|
|
$end = self::convertToDateTime($values[1]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return [$start, $end]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function excludeStartDate() |
58
|
|
|
{ |
59
|
|
|
$this->excludeStartDate = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function excludeEndDate() |
63
|
|
|
{ |
64
|
|
|
$this->excludeEndDate = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setInterval(DateInterval $interval) |
68
|
|
|
{ |
69
|
|
|
$this->interval = $interval; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getInterval() |
73
|
|
|
{ |
74
|
|
|
return $this->interval; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getDatePeriod(DateInterval $interval = null) |
78
|
|
|
{ |
79
|
|
|
if (!$this->excludeEndDate) { |
80
|
|
|
$end = clone $this->end; |
81
|
|
|
// DatePeriod does not include end date so, plus 1 sec to end date. |
82
|
|
|
$end->modify('+1 sec'); |
83
|
|
|
} else { |
84
|
|
|
$end = $this->end; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$option = null; |
88
|
|
|
if ($this->excludeStartDate) { |
89
|
|
|
$option = DatePeriod::EXCLUDE_START_DATE; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new DatePeriod($this->start, ($interval) ?: $this->interval, $end, $option); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getIterator() |
96
|
|
|
{ |
97
|
|
|
return $this->getDatePeriod(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function convertToDateTime($param) |
101
|
|
|
{ |
102
|
|
|
if ($param instanceOf DateTime) { |
103
|
|
|
return $param; |
104
|
|
|
} |
105
|
|
|
if (strtotime($param) === false) { |
106
|
|
|
throw new \InvalidArgumentException('Invalid datetime string'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return new DateTime($param); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getStart() |
113
|
|
|
{ |
114
|
|
|
return $this->start; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getEnd() |
118
|
|
|
{ |
119
|
|
|
return $this->end; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function contains($dateString) |
123
|
|
|
{ |
124
|
|
|
$date = self::convertToDateTime($dateString); |
125
|
|
|
$timestamp = $date->getTimestamp(); |
126
|
|
|
|
127
|
|
|
if ($this->excludeStartDate) { |
128
|
|
|
$isAfterThanStart = $this->start->getTimestamp() < $timestamp; |
129
|
|
|
} else { |
130
|
|
|
$isAfterThanStart = $this->start->getTimestamp() <= $timestamp; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($this->excludeEndDate) { |
134
|
|
|
$isBeforeThanEnd = $timestamp < $this->end->getTimestamp(); |
135
|
|
|
} else { |
136
|
|
|
$isBeforeThanEnd = $timestamp <= $this->end->getTimestamp(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
return $isAfterThanStart && $isBeforeThanEnd; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|