-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcalendar_en.php
151 lines (151 loc) · 6.29 KB
/
calendar_en.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
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @author Xu Ding
* @website https://www.StarTutorial.com
* @revised by Alessandro Marinuzzi
* @website https://www.alecos.it/
* @revised 16.11.2020 • UTF-8 •
**/
ini_set('default_charset', 'UTF-8');
class Calendar {
/**
** Constructor
**/
public function __construct() {
$this->naviHref = htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES | ENT_HTML401, 'UTF-8');
$this->longMonth = 0; /* 0 for short month - 1 for long month */
}
/********************* PROPERTY ********************/
private $dayLabels = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
private $currentYear = 0;
private $currentMonth = 0;
private $currentDay = 0;
private $currentDate = null;
private $daysInMonth = 0;
private $naviHref = null;
private $longMonth = 0;
/********************* PUBLIC **********************/
/**
** Print out the calendar
**/
public function show() {
$year = null;
$month = null;
if (null == $year && isset($_GET['year'])) {
$year = htmlentities($_GET['year'], ENT_QUOTES | ENT_HTML401, 'UTF-8');
} elseif (null == $year) {
$year = date("Y", time());
}
if ((!is_numeric($year)) || ($year == "")) {
$year = date("Y", time());
}
if (null == $month && isset($_GET['month'])) {
$month = htmlentities($_GET['month'], ENT_QUOTES | ENT_HTML401, 'UTF-8');
} elseif (null == $month) {
$month = date("m", time());
}
if ((!is_numeric($month)) || ($month == "")) {
$month = date("m", time());
}
$this->currentYear = $year;
$this->currentMonth = $month;
$this->daysInMonth = $this->_daysInMonth($month, $year);
$content = '<div id="calendar">' . "\r\n" . '<div class="calendar_box">' . "\r\n" . $this->_createNavi() . "\r\n" . '</div>' . "\r\n" . '<div class="calendar_content">' . "\r\n" . '<div class="calendar_label">' . "\r\n" . $this->_createLabels() . '</div>' . "\r\n";
$content .= '<div class="calendar_clear"></div>' . "\r\n";
$content .= '<div class="calendar_dates">' . "\r\n";
$weeksInMonth = $this->_weeksInMonth($month, $year);
// Create weeks in a month
for ($i = 0; $i < $weeksInMonth; $i++) {
// Create days in a week
for ($j = 1; $j <= 7; $j++) {
$content .= $this->_showDay($i * 7 + $j);
}
}
$content .= '</div>' . "\r\n";
$content .= '<div class="calendar_clear"></div>' . "\r\n";
$content .= '</div>' . "\r\n";
$content .= '</div>' . "\r\n";
return $content;
}
/********************* PRIVATE **********************/
/**
** Create the calendar days
**/
private function _showDay($cellNumber) {
if ($this->currentDay == 0) {
$firstDayOfTheWeek = date('N', strtotime($this->currentYear . '-' . $this->currentMonth . '-01'));
if (intval($cellNumber) == intval($firstDayOfTheWeek)) {
$this->currentDay = 1;
}
}
if (($this->currentDay != 0) && ($this->currentDay <= $this->daysInMonth)) {
$this->currentDate = date('Y-m-d', strtotime($this->currentYear . '-' . $this->currentMonth . '-' . ($this->currentDay)));
$cellContent = $this->currentDay;
$this->currentDay++;
} else {
$this->currentDate = null;
$cellContent = null;
}
$today_day = date("d");
$today_mon = date("m");
$today_yea = date("Y");
$class_day = ($cellContent == $today_day && $this->currentMonth == $today_mon && $this->currentYear == $today_yea ? "calendar_today" : "calendar_days");
return '<div class="' . $class_day . '">' . $cellContent . '</div>' . "\r\n";
}
/**
** Create navigation
**/
private function _createNavi() {
$nextMonth = $this->currentMonth == 12 ? 1 : intval($this->currentMonth)+1;
$nextYear = $this->currentMonth == 12 ? intval($this->currentYear)+1 : $this->currentYear;
$preMonth = $this->currentMonth == 1 ? 12 : intval($this->currentMonth)-1;
$preYear = $this->currentMonth == 1 ? intval($this->currentYear)-1 : $this->currentYear;
if ($this->longMonth == 1) {
$thisMonth = date('Y F', strtotime($this->currentYear . '-' . $this->currentMonth . '-1'));
} else {
$thisMonth = date('Y M', strtotime($this->currentYear . '-' . $this->currentMonth . '-1'));
}
return '<div class="calendar_header">' . "\r\n" . '<a class="calendar_prev" href="' . $this->naviHref . '?month=' . sprintf('%02d', $preMonth) . '&year=' . $preYear.'">Prev</a>' . "\r\n" . '<span class="calendar_title">' . $thisMonth . '</span>' . "\r\n" . '<a class="calendar_next" href="' . $this->naviHref . '?month=' . sprintf("%02d", $nextMonth) . '&year=' . $nextYear . '">Next</a>' . "\r\n" . '</div>';
}
/**
** Create calendar week labels
**/
private function _createLabels() {
$content = '';
foreach ($this->dayLabels as $index => $label) {
$content .= '<div class="calendar_names">' . $label . '</div>' . "\r\n";
}
return $content;
}
/**
** Calculate number of weeks in a particular month
**/
private function _weeksInMonth($month = null, $year = null) {
if (null == ($year)) {
$year = date("Y", time());
}
if (null == ($month)) {
$month = date("m", time());
}
// Find number of days in this month
$daysInMonths = $this->_daysInMonth($month, $year);
$numOfweeks = ($daysInMonths % 7 == 0 ? 0 : 1) + intval($daysInMonths / 7);
$monthEndingDay = date('N',strtotime($year . '-' . $month . '-' . $daysInMonths));
$monthStartDay = date('N',strtotime($year . '-' . $month . '-01'));
if ($monthEndingDay < $monthStartDay) {
$numOfweeks++;
}
return $numOfweeks;
}
/**
** Calculate number of days in a particular month
**/
private function _daysInMonth($month = null, $year = null) {
if (null == ($year)) $year = date("Y",time());
if (null == ($month)) $month = date("m",time());
return date('t', strtotime($year . '-' . $month . '-01'));
}
}
$calendar = new Calendar();
echo $calendar->show();
?>