1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CL\DateUtils; |
4
|
|
|
|
5
|
|
|
use DateInterval; |
6
|
|
|
use DatePeriod; |
7
|
|
|
use DateTime; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Ivan Kerin <[email protected]> |
11
|
|
|
* @copyright 2015, Clippings Ltd. |
12
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
13
|
|
|
*/ |
14
|
|
|
class DateTimeSpan |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DateTime |
18
|
|
|
*/ |
19
|
|
|
private $from; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DateTime |
23
|
|
|
*/ |
24
|
|
|
private $to; |
25
|
|
|
|
26
|
1 |
|
public function __construct(DateTime $from, DateTime $to) |
27
|
|
|
{ |
28
|
1 |
|
$this->from = $from; |
29
|
1 |
|
$this->to = $to; |
30
|
1 |
|
} |
31
|
|
|
|
32
|
1 |
|
public function getFrom(): DateTime |
33
|
|
|
{ |
34
|
1 |
|
return $this->from; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function getTo(): DateTime |
38
|
|
|
{ |
39
|
1 |
|
return $this->to; |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
public function contains(DateTime $datetime): bool |
43
|
|
|
{ |
44
|
3 |
|
return $this->from < $datetime and $this->to > $datetime; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
public function humanize(): string |
48
|
|
|
{ |
49
|
4 |
|
$from = $this->getFrom(); |
50
|
4 |
|
$to = $this->getTo(); |
51
|
|
|
|
52
|
4 |
|
if ($from->format('Y') == $to->format('Y')) { |
53
|
3 |
|
if ($from->format('m') == $to->format('m')) { |
54
|
2 |
|
if ($from->format('d') == $to->format('d')) { |
55
|
1 |
|
return $from->format('j M Y'); |
56
|
|
|
} else { |
57
|
1 |
|
return $from->format('j').' - '.$to->format('j').' '.$from->format('M Y'); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
1 |
|
return $from->format('j M').' - '.$to->format('j M').' '.$from->format('Y'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return $from->format('j M Y').' - '.$to->format('j M Y'); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
public function getBusinessDaysInPeriodFrom(DateTime $startDate = null): int |
68
|
|
|
{ |
69
|
6 |
|
if ($startDate === null) { |
70
|
1 |
|
$startDate = new DateTime('today'); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
return $this->calculateBusinessDaysInPeriod($startDate, $this->from); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getBusinessDaysInPeriodTo(DateTime $startDate = null): int |
77
|
|
|
{ |
78
|
|
|
if ($startDate === null) { |
79
|
|
|
$startDate = new DateTime('today'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->calculateBusinessDaysInPeriod($startDate, $this->to); |
83
|
|
|
} |
84
|
|
|
|
85
|
12 |
|
private function calculateBusinessDaysInPeriod(DateTime $startDate, DateTime $endDate): int |
86
|
|
|
{ |
87
|
12 |
|
$interval = new DateInterval('P1D'); // 1 Day |
88
|
12 |
|
$dateRange = new DatePeriod($startDate, $interval, $endDate); |
89
|
12 |
|
$workingDays = 0; |
90
|
12 |
|
foreach ($dateRange as $date) { |
91
|
6 |
|
if ($date->format('N') <= 5) { |
92
|
6 |
|
++$workingDays; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
12 |
|
return $workingDays; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|