forked from vegvari/CalendarTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar.php
140 lines (115 loc) · 3.09 KB
/
Calendar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Calendar;
use DateTimeInterface;
class Calendar implements CalendarInterface
{
private $dateTime;
/**
* @param DateTimeInterface $dateTime
*/
public function __construct(DateTimeInterface $dateTime)
{
$this->dateTime = $dateTime;
}
/**
* Get the day
*
* @return int
*/
public function getDay()
{
return (int)(clone $this->dateTime)->format('d');
}
/**
* Get the weekday (1-7, 1 = Monday)
*
* @return int
*/
public function getWeekDay()
{
return (int)(clone $this->dateTime)->format('N');
}
/**
* Get the first weekday of this month (1-7, 1 = Monday)
*
* @return int
*/
public function getFirstWeekDay()
{
return (int)(clone $this->dateTime)
->modify('first day of this month')
->format('N');
}
/**
* Get the first week of this month (18th March => 9 because March starts on week 9)
*
* @return int
*/
public function getFirstWeek()
{
return (int)(clone $this->dateTime)
->modify('first day of this month')
->format('W');
}
/**
* Get the number of days in this month
*
* @return int
*/
public function getNumberOfDaysInThisMonth()
{
return (int)(clone $this->dateTime)
->modify('last day of this month')
->format('d');
}
/**
* Get the number of days in the previous month
*
* @return int
*/
public function getNumberOfDaysInPreviousMonth()
{
return (int)(clone $this->dateTime)
->modify('last day of previous month')
->format('d');
}
/**
* Get the calendar array
*
* @return array
*/
public function getCalendar()
{
$firstDayOfMonth = (clone $this->dateTime)->modify('first day of this month');
$calendarDay = (clone $firstDayOfMonth)->modify('first monday of this week');
$calendar = [];
for ($w=0; $w<$this->getNumberOfWeeksInThisMonth(); $w++) {
$week = [];
$weekNo = (int)$calendarDay->format('W');
$shouldHighlight = $this->shouldHighlight($calendarDay);
for ($i=0; $i<7; $i++) {
$week[(int)$calendarDay->format('d')] = $shouldHighlight;
$calendarDay->modify('+1 day');
}
$calendar[$weekNo] = $week;
}
return $calendar;
}
private function getNumberOfWeeksInThisMonth()
{
$firstDay = (clone $this->dateTime)
->modify('first day of this month')
->modify('first monday of this week');
$lastDay = (clone $this->dateTime)
->modify('last day of this month')
->modify('sunday');
return $lastDay->diff($firstDay, true)->days/7;
}
private function shouldHighlight(DateTimeInterface $day)
{
return (
$this->dateTime >= (clone $day)->modify('+1 week') &&
$this->dateTime < (clone $day)->modify('+2 week')
);
}
}