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