|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mos\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 $lastDayInMonth; |
|
21
|
|
|
private $thisMonth; |
|
22
|
|
|
private $prevMonth; |
|
23
|
|
|
private $nextMonth; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor |
|
27
|
|
|
* |
|
28
|
|
|
* @param string date in month to display. Format yyyy-mm-dd |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($displayDate = null) |
|
32
|
|
|
{ |
|
33
|
|
|
setlocale(LC_TIME, "Swedish"); |
|
34
|
|
|
define("CHARSET", "iso-8859-1"); |
|
35
|
|
|
|
|
36
|
|
|
if (null == $displayDate) { |
|
37
|
|
|
$date = new DateTime(); |
|
38
|
|
|
} else { |
|
39
|
|
|
$date = new DateTime($displayDate); |
|
40
|
|
|
} |
|
41
|
|
|
$this->today = new DateTime(); |
|
42
|
|
|
// Remove time, only keep date when we compare later. |
|
43
|
|
|
$this->today->setTime(0, 0); |
|
44
|
|
|
|
|
45
|
|
|
// Find first week and day in month and first day in that week |
|
46
|
|
|
$year = $date->format('Y'); |
|
47
|
|
|
$month = $date->format('m'); |
|
48
|
|
|
$this->firstDayInMonth = new DateTime(); |
|
|
|
|
|
|
49
|
|
|
$this->firstDayInMonth->setTime(0, 0); |
|
50
|
|
|
$this->firstDayInMonth->setDate($year, $month, 1); |
|
51
|
|
|
$dayOfWeek = $this->firstDayInMonth->format('N'); |
|
52
|
|
|
$subtractDays = $dayOfWeek - 1; |
|
53
|
|
|
$this->firstDayInWeekOfMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
|
54
|
|
|
$this->firstDayInWeekOfMonth->modify("-{$subtractDays} day"); |
|
55
|
|
|
|
|
56
|
|
|
// Find last week and day in month and last day in that week |
|
57
|
|
|
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); |
|
58
|
|
|
$this->lastDayInMonth = new DateTime("$year-$month-$daysInMonth"); |
|
59
|
|
|
$this->lastDayInMonth->setTime(0, 0); |
|
60
|
|
|
$dayOfWeek = $this->lastDayInMonth->format('N'); |
|
61
|
|
|
$addDays = 7 - $dayOfWeek; |
|
62
|
|
|
$this->lastDayInLastWeek = new DateTime($this->lastDayInMonth->format('Y-m-d')); |
|
63
|
|
|
$this->lastDayInLastWeek->modify("+$addDays day"); |
|
64
|
|
|
|
|
65
|
|
|
$this->thisMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
|
66
|
|
|
$this->prevMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
|
67
|
|
|
$this->prevMonth->modify('-1 month'); |
|
68
|
|
|
$this->nextMonth = new DateTime($this->firstDayInMonth->format('Y-m-d')); |
|
69
|
|
|
$this->nextMonth->modify('+1 month'); |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
public function today() |
|
76
|
|
|
{ |
|
77
|
|
|
$today = new DateTime(); |
|
78
|
|
|
$out = utf8_encode(strftime("%A %d %B %Y", strtotime($today->format('Y-m-d')))); |
|
79
|
|
|
return $out; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get current month with month and year |
|
84
|
|
|
*/ |
|
85
|
|
|
public function thisMonth() |
|
86
|
|
|
{ |
|
87
|
|
|
return utf8_encode(strftime("%B %Y", strtotime($this->thisMonth->format('Y-m-d')))); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get previous month in text format |
|
92
|
|
|
*/ |
|
93
|
|
|
public function prevMonth() |
|
94
|
|
|
{ |
|
95
|
|
|
return utf8_encode(strftime("%B", strtotime($this->prevMonth->format('Y-m-d')))); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get previous month in format yyyy-mm-dd |
|
100
|
|
|
*/ |
|
101
|
|
|
public function prevMonthDate() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->prevMonth->format('Y-m-d'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get next month in text format |
|
108
|
|
|
*/ |
|
109
|
|
|
public function nextMonth() |
|
110
|
|
|
{ |
|
111
|
|
|
return utf8_encode(strftime("%B", strtotime($this->nextMonth->format('Y-m-d')))); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get next month in format yyyy-mm-dd |
|
116
|
|
|
*/ |
|
117
|
|
|
public function nextMonthDate() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->nextMonth->format('Y-m-d'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get all dates in month with additional days in first and last week. |
|
124
|
|
|
* |
|
125
|
|
|
*/ |
|
126
|
|
|
public function datesInMonth() |
|
127
|
|
|
{ |
|
128
|
|
|
$date = $this->firstDayInWeekOfMonth; |
|
129
|
|
|
while ($date <= $this->lastDayInLastWeek) { |
|
130
|
|
|
$w = ltrim($date->format('W'), 0); |
|
131
|
|
|
for ($d=1; $d < 8; $d++) { |
|
132
|
|
|
// $dateText = utf8_encode(strftime("%A %e %B", strtotime($date->format('Y-m-d')))); |
|
133
|
|
|
// %e does not work on windows, use %d instead |
|
134
|
|
|
$dateText = utf8_encode(strftime("%A %#d %B", strtotime($date->format('Y-m-d')))); |
|
|
|
|
|
|
135
|
|
|
$dateText = $date->format('d M'); |
|
136
|
|
|
// $dateText = $date->format('D d M'); |
|
137
|
|
|
$redDay = (7==$d) ? "red-day" : ""; |
|
138
|
|
|
$classToday = ($date == $this->today) ? "today" : ""; |
|
139
|
|
|
$classThisMonth = ($date < $this->firstDayInMonth || $date > $this->lastDayInMonth) ? "class-outside-month" : "class-inside-month"; |
|
140
|
|
|
$date->modify("+1 day"); |
|
141
|
|
|
$dates[] = array( |
|
|
|
|
|
|
142
|
|
|
'week' => $w, |
|
143
|
|
|
'date' => $dateText, |
|
144
|
|
|
'class-red' => $redDay, |
|
145
|
|
|
'class-today' => $classToday, |
|
146
|
|
|
'class-in-month' => $classThisMonth, |
|
147
|
|
|
); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
return $dates; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: