1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fnlive\Calendar; |
4
|
|
|
|
5
|
|
|
use \DateTime; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class for calendar function |
9
|
|
|
*/ |
10
|
|
|
class CCalendar |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Properties |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
private $today; |
18
|
|
|
private $firstDayInWeekOfMonth; |
19
|
|
|
private $lastDayInLastWeek; |
20
|
|
|
private $firstDayInMonth; |
21
|
|
|
private $lastDayInMonth; |
22
|
|
|
private $thisMonth; |
23
|
|
|
private $prevMonth; |
24
|
|
|
private $nextMonth; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* |
29
|
|
|
* @param string date in month to display. Format yyyy-mm-dd |
30
|
|
|
* |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct($displayDate = null) |
33
|
|
|
{ |
34
|
|
|
// setlocale(LC_TIME, "Swedish"); |
|
|
|
|
35
|
|
|
// define("CHARSET", "iso-8859-1"); |
|
|
|
|
36
|
|
|
|
37
|
4 |
|
if (null == $displayDate) { |
38
|
1 |
|
$date = new DateTime(); |
39
|
1 |
|
} else { |
40
|
3 |
|
$date = new DateTime($displayDate); |
41
|
|
|
} |
42
|
4 |
|
$this->today = new DateTime(); |
43
|
|
|
// Remove time, only keep date when we compare later. |
44
|
4 |
|
$this->today->setTime(0, 0); |
45
|
|
|
|
46
|
|
|
// Find first week and day in month and first day in that week |
47
|
4 |
|
$year = $date->format('Y'); |
48
|
4 |
|
$month = $date->format('m'); |
49
|
4 |
|
$this->firstDayInMonth = new DateTime(); |
50
|
4 |
|
$this->firstDayInMonth->setTime(0, 0); |
51
|
4 |
|
$this->firstDayInMonth->setDate($year, $month, 1); |
52
|
4 |
|
$dayOfWeek = $this->firstDayInMonth->format('N'); |
53
|
4 |
|
$subtractDays = $dayOfWeek - 1; |
54
|
4 |
|
$this->firstDayInWeekOfMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
55
|
4 |
|
$this->firstDayInWeekOfMonth->modify("-{$subtractDays} day"); |
56
|
|
|
|
57
|
|
|
// Find last week and day in month and last day in that week |
58
|
4 |
|
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); |
59
|
4 |
|
$this->lastDayInMonth = new DateTime("$year-$month-$daysInMonth"); |
60
|
4 |
|
$this->lastDayInMonth->setTime(0, 0); |
61
|
4 |
|
$dayOfWeek = $this->lastDayInMonth->format('N'); |
62
|
4 |
|
$addDays = 7 - $dayOfWeek; |
63
|
4 |
|
$this->lastDayInLastWeek = new DateTime($this->lastDayInMonth->format('Y-m-d')); |
64
|
4 |
|
$this->lastDayInLastWeek->modify("+$addDays day"); |
65
|
|
|
|
66
|
4 |
|
$this->thisMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
67
|
4 |
|
$this->prevMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
68
|
4 |
|
$this->prevMonth->modify('-1 month'); |
69
|
4 |
|
$this->nextMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
70
|
4 |
|
$this->nextMonth->modify('+1 month'); |
71
|
|
|
|
72
|
|
|
|
73
|
4 |
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
1 |
|
public function today() |
77
|
|
|
{ |
78
|
1 |
|
$today = new DateTime(); |
79
|
1 |
|
$out = utf8_encode(strftime("%A %d %B %Y", strtotime($today->format('Y-m-d')))); |
80
|
1 |
|
return $out; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get current month with month and year |
85
|
|
|
*/ |
86
|
1 |
|
public function thisMonth() |
87
|
|
|
{ |
88
|
1 |
|
return utf8_encode(strftime("%B %Y", strtotime($this->thisMonth->format('Y-m-d')))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get previous month in text format |
93
|
|
|
*/ |
94
|
1 |
|
public function prevMonth() |
95
|
|
|
{ |
96
|
1 |
|
return utf8_encode(strftime("%B", strtotime($this->prevMonth->format('Y-m-d')))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get previous month in format yyyy-mm-dd |
101
|
|
|
*/ |
102
|
1 |
|
public function prevMonthDate() |
103
|
|
|
{ |
104
|
1 |
|
return $this->prevMonth->format('Y-m-d'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get next month in text format |
109
|
|
|
*/ |
110
|
1 |
|
public function nextMonth() |
111
|
|
|
{ |
112
|
1 |
|
return utf8_encode(strftime("%B", strtotime($this->nextMonth->format('Y-m-d')))); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get next month in format yyyy-mm-dd |
117
|
|
|
*/ |
118
|
1 |
|
public function nextMonthDate() |
119
|
|
|
{ |
120
|
1 |
|
return $this->nextMonth->format('Y-m-d'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get all dates in month with additional days in first and last week. |
125
|
|
|
* |
126
|
|
|
*/ |
127
|
1 |
|
public function datesInMonth() |
128
|
|
|
{ |
129
|
1 |
|
$dates = array(); |
130
|
1 |
|
$date = $this->firstDayInWeekOfMonth; |
131
|
1 |
|
while ($date <= $this->lastDayInLastWeek) { |
132
|
1 |
|
$w = ltrim($date->format('W'), 0); |
133
|
1 |
|
for ($d=1; $d < 8; $d++) { |
134
|
1 |
|
$dateText = $date->format('d M'); |
135
|
1 |
|
$redDay = (7==$d) ? "red-day" : ""; |
136
|
1 |
|
$classToday = ($date == $this->today) ? "today" : ""; |
137
|
1 |
|
$classThisMonth = ($date < $this->firstDayInMonth || $date > $this->lastDayInMonth) ? "class-outside-month" : "class-inside-month"; |
138
|
1 |
|
$date->modify("+1 day"); |
139
|
1 |
|
$dates[] = array( |
140
|
1 |
|
'week' => $w, |
141
|
1 |
|
'date' => $dateText, |
142
|
1 |
|
'class-red' => $redDay, |
143
|
1 |
|
'class-today' => $classToday, |
144
|
1 |
|
'class-in-month' => $classThisMonth, |
145
|
|
|
); |
146
|
1 |
|
} |
147
|
1 |
|
} |
148
|
1 |
|
return $dates; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.