1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Helper class that provides useful php functions. |
4
|
|
|
* |
5
|
|
|
* @author Vettivel Satheez <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/satheez |
8
|
|
|
* |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sa\Helper; |
13
|
|
|
|
14
|
|
|
use DateInterval; |
15
|
|
|
use DatePeriod; |
16
|
|
|
use DateTime; |
17
|
|
|
|
18
|
|
|
class Date |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Prepare datetime object by week and year. |
22
|
|
|
* |
23
|
|
|
* @param int $year |
24
|
|
|
* @param int $week |
25
|
|
|
* |
26
|
|
|
* @throws \Exception |
27
|
|
|
* |
28
|
|
|
* @return DateTime |
29
|
|
|
*/ |
30
|
|
|
public static function prepareDatetimeObjectByWeekAndYear(int $year, int $week): DateTime |
31
|
|
|
{ |
32
|
|
|
$dto = new DateTime(); |
33
|
|
|
$dto->setISODate($year, $week); |
34
|
|
|
|
35
|
|
|
return $dto; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get week start and end dates by week number. |
40
|
|
|
* |
41
|
|
|
* @param int $year |
42
|
|
|
* @param int $week |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
* |
48
|
|
|
* @see https://stackoverflow.com/questions/4861384/php-get-start-and-end-date-of-a-week-by-weeknumber |
49
|
|
|
*/ |
50
|
|
|
public static function getWeekStartAndEndDatesByWeekNumber(int $year, int $week): array |
51
|
|
|
{ |
52
|
|
|
$res = []; |
53
|
|
|
$dto = self::prepareDatetimeObjectByWeekAndYear($year, $week); |
54
|
|
|
$res['week_start'] = $dto->format('Y-m-d'); |
55
|
|
|
$dto->modify('+6 days'); |
56
|
|
|
$res['week_end'] = $dto->format('Y-m-d'); |
57
|
|
|
|
58
|
|
|
return $res; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get last week number of a year. |
63
|
|
|
* |
64
|
|
|
* @param int $year |
65
|
|
|
* |
66
|
|
|
* @throws \Exception |
67
|
|
|
* |
68
|
|
|
* @return int |
69
|
|
|
* |
70
|
|
|
* @see https://stackoverflow.com/questions/3319386/php-get-last-week-number-in-year#9018728 |
71
|
|
|
*/ |
72
|
|
|
public static function getLastWeekOfTheYear(int $year) |
73
|
|
|
{ |
74
|
|
|
$dto = self::prepareDatetimeObjectByWeekAndYear($year, 53); |
75
|
|
|
|
76
|
|
|
return $dto->format('W') === '53' ? 53 : 52; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all dates between two Dates. |
81
|
|
|
* |
82
|
|
|
* @param string|DateTime $startDate |
83
|
|
|
* @param string|DateTime $endDate |
84
|
|
|
* |
85
|
|
|
* @throws \Exception |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
* |
89
|
|
|
* @see https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array |
90
|
|
|
*/ |
91
|
|
|
public static function getAllDatesBetweenTwoDates($startDate, $endDate): array |
92
|
|
|
{ |
93
|
|
|
if (!($startDate instanceof DateTime)) { |
94
|
|
|
$startDate = new DateTime($startDate); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!($endDate instanceof DateTime)) { |
98
|
|
|
$endDate = new DateTime($endDate); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$period = new DatePeriod($startDate, new DateInterval('P1D'), $endDate); |
102
|
|
|
$dates = []; |
103
|
|
|
|
104
|
|
|
if ($startDate > $endDate) { |
105
|
|
|
return $dates; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
foreach ($period as $key => $value) { |
109
|
|
|
$dates[] = $value->format('Y-m-d'); |
110
|
|
|
} |
111
|
|
|
$dates[] = $endDate->format('Y-m-d'); |
112
|
|
|
|
113
|
|
|
return $dates; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check whether the given year is leaf year or not. |
118
|
|
|
* |
119
|
|
|
* @param int $year |
120
|
|
|
* |
121
|
|
|
* @throws \Exception |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public static function isLeafYear(int $year) : bool |
126
|
|
|
{ |
127
|
|
|
$dto = self::prepareDatetimeObjectByWeekAndYear($year, 5); |
128
|
|
|
|
129
|
|
|
return !empty($dto->format('L')); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|