|
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 DateTime; |
|
15
|
|
|
use DatePeriod; |
|
16
|
|
|
use DateInterval; |
|
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
|
|
|
* @return DateTime |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function prepareDatetimeObjectByWeekAndYear(int $year, int $week): DateTime |
|
30
|
|
|
{ |
|
31
|
|
|
$dto = new DateTime(); |
|
32
|
|
|
$dto->setISODate($year, $week); |
|
33
|
|
|
return $dto; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get week start and end dates by week number |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $year |
|
40
|
|
|
* @param int $week |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws \Exception |
|
44
|
|
|
* @see https://stackoverflow.com/questions/4861384/php-get-start-and-end-date-of-a-week-by-weeknumber |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getWeekStartAndEndDatesByWeekNumber(int $year, int $week): array |
|
47
|
|
|
{ |
|
48
|
|
|
$res = []; |
|
49
|
|
|
$dto = self::prepareDatetimeObjectByWeekAndYear($year, $week); |
|
50
|
|
|
$res['week_start'] = $dto->format('Y-m-d'); |
|
51
|
|
|
$dto->modify('+6 days'); |
|
52
|
|
|
$res['week_end'] = $dto->format('Y-m-d'); |
|
53
|
|
|
return $res; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get last week number of a year |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $year |
|
60
|
|
|
* |
|
61
|
|
|
* @return int |
|
62
|
|
|
* @throws \Exception |
|
63
|
|
|
* @see https://stackoverflow.com/questions/3319386/php-get-last-week-number-in-year#9018728 |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function getLastWeekOfTheYear(int $year) |
|
66
|
|
|
{ |
|
67
|
|
|
$dto = self::prepareDatetimeObjectByWeekAndYear($year, 53); |
|
68
|
|
|
return $dto->format('W') === '53' ? 53 : 52; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get all dates between two Dates |
|
73
|
|
|
* |
|
74
|
|
|
* @param string|DateTime $startDate |
|
75
|
|
|
* @param string|DateTime $endDate |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
* @see https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function getAllDatesBetweenTwoDates($startDate, $endDate): array |
|
82
|
|
|
{ |
|
83
|
|
|
if (!($startDate instanceof DateTime)) { |
|
84
|
|
|
$startDate = new DateTime($startDate); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (!($endDate instanceof DateTime)) { |
|
88
|
|
|
$endDate = new DateTime($endDate); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$period = new DatePeriod($startDate, new DateInterval('P1D'), $endDate); |
|
92
|
|
|
$dates = []; |
|
93
|
|
|
|
|
94
|
|
|
if ($startDate > $endDate) { |
|
95
|
|
|
return $dates; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
foreach ($period as $key => $value) { |
|
99
|
|
|
$dates[] = $value->format('Y-m-d'); |
|
100
|
|
|
} |
|
101
|
|
|
$dates[] = $endDate->format('Y-m-d'); |
|
102
|
|
|
return $dates; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Check whether the given year is leaf year or not |
|
107
|
|
|
* |
|
108
|
|
|
* @param int $year |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool |
|
111
|
|
|
* @throws \Exception |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function isLeafYear(int $year) : bool |
|
114
|
|
|
{ |
|
115
|
|
|
$dto = self::prepareDatetimeObjectByWeekAndYear($year, 5); |
|
116
|
|
|
return !empty($dto->format('L')); |
|
117
|
|
|
} |
|
118
|
|
|
} |