1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\OpeningHours\Helpers; |
4
|
|
|
|
5
|
|
|
use DateTimeInterface; |
6
|
|
|
|
7
|
|
|
trait DiffTrait |
8
|
|
|
{ |
9
|
|
|
private function diffInSeconds(string $stateCheckMethod, string $nextDateMethod, string $skipDateMethod, DateTimeInterface $startDate, DateTimeInterface $endDate): float |
10
|
|
|
{ |
11
|
|
|
$time = 0; |
12
|
|
|
|
13
|
|
|
if ($endDate < $startDate) { |
14
|
|
|
return -$this->diffInSeconds($stateCheckMethod, $nextDateMethod, $skipDateMethod, $endDate, $startDate); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$date = $startDate; |
18
|
|
|
|
19
|
|
|
while ($date < $endDate) { |
20
|
|
|
if ($this->$stateCheckMethod($date)) { |
21
|
|
|
$date = $this->$skipDateMethod($date); |
22
|
|
|
continue; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$nextDate = min($endDate, $this->$nextDateMethod($date)); |
26
|
|
|
$time += floatval($nextDate->format('U.u')) - floatval($date->format('U.u')); |
27
|
|
|
$date = $nextDate; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $time; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return the amount of open time (number of seconds as a floating number) between 2 dates/times. |
35
|
|
|
* |
36
|
|
|
* @param DateTimeInterface $startDate |
37
|
|
|
* @param DateTimeInterface $endDate |
38
|
|
|
* |
39
|
|
|
* @return float |
40
|
|
|
*/ |
41
|
|
|
public function diffInOpenSeconds(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
42
|
|
|
{ |
43
|
|
|
return $this->diffInSeconds('isClosedAt', 'nextClose', 'nextOpen', $startDate, $endDate); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the amount of open time (number of minutes as a floating number) between 2 dates/times. |
48
|
|
|
* |
49
|
|
|
* @param DateTimeInterface $startDate |
50
|
|
|
* @param DateTimeInterface $endDate |
51
|
|
|
* |
52
|
|
|
* @return float |
53
|
|
|
*/ |
54
|
|
|
public function diffInOpenMinutes(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
55
|
|
|
{ |
56
|
|
|
return $this->diffInOpenSeconds($startDate, $endDate) / 60; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return the amount of open time (number of hours as a floating number) between 2 dates/times. |
61
|
|
|
* |
62
|
|
|
* @param DateTimeInterface $startDate |
63
|
|
|
* @param DateTimeInterface $endDate |
64
|
|
|
* |
65
|
|
|
* @return float |
66
|
|
|
*/ |
67
|
|
|
public function diffInOpenHours(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
68
|
|
|
{ |
69
|
|
|
return $this->diffInOpenMinutes($startDate, $endDate) / 60; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return the amount of closed time (number of seconds as a floating number) between 2 dates/times. |
74
|
|
|
* |
75
|
|
|
* @param DateTimeInterface $startDate |
76
|
|
|
* @param DateTimeInterface $endDate |
77
|
|
|
* |
78
|
|
|
* @return float |
79
|
|
|
*/ |
80
|
|
|
public function diffInClosedSeconds(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
81
|
|
|
{ |
82
|
|
|
return $this->diffInSeconds('isOpenAt', 'nextOpen', 'nextClose', $startDate, $endDate); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the amount of closed time (number of minutes as a floating number) between 2 dates/times. |
87
|
|
|
* |
88
|
|
|
* @param DateTimeInterface $startDate |
89
|
|
|
* @param DateTimeInterface $endDate |
90
|
|
|
* |
91
|
|
|
* @return float |
92
|
|
|
*/ |
93
|
|
|
public function diffInClosedMinutes(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
94
|
|
|
{ |
95
|
|
|
return $this->diffInClosedSeconds($startDate, $endDate) / 60; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return the amount of closed time (number of hours as a floating number) between 2 dates/times. |
100
|
|
|
* |
101
|
|
|
* @param DateTimeInterface $startDate |
102
|
|
|
* @param DateTimeInterface $endDate |
103
|
|
|
* |
104
|
|
|
* @return float |
105
|
|
|
*/ |
106
|
|
|
public function diffInClosedHours(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
107
|
|
|
{ |
108
|
|
|
return $this->diffInClosedMinutes($startDate, $endDate) / 60; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|