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