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
|
|
|
public function diffInOpenSeconds(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
34
|
|
|
{ |
35
|
|
|
return $this->diffInSeconds('isClosedAt', 'nextClose', 'nextOpen', $startDate, $endDate); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function diffInOpenMinutes(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
39
|
|
|
{ |
40
|
|
|
return $this->diffInOpenSeconds($startDate, $endDate) / 60; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function diffInOpenHours(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
44
|
|
|
{ |
45
|
|
|
return $this->diffInOpenMinutes($startDate, $endDate) / 60; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function diffInClosedSeconds(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
49
|
|
|
{ |
50
|
|
|
return $this->diffInSeconds('isOpenAt', 'nextOpen', 'nextClose', $startDate, $endDate); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function diffInClosedMinutes(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
54
|
|
|
{ |
55
|
|
|
return $this->diffInClosedSeconds($startDate, $endDate) / 60; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function diffInClosedHours(DateTimeInterface $startDate, DateTimeInterface $endDate): float |
59
|
|
|
{ |
60
|
|
|
return $this->diffInClosedMinutes($startDate, $endDate) / 60; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|