回答編集履歴

1

追記

2018/09/21 14:44

投稿

退会済みユーザー
test CHANGED
@@ -15,3 +15,323 @@
15
15
 
16
16
 
17
17
  コードを書いているときにリアルタイムに指摘してくれるので、こんな質問をしなくて済むようになりますよ、
18
+
19
+
20
+
21
+ ---
22
+
23
+
24
+
25
+ ```php
26
+
27
+ <?php
28
+
29
+
30
+
31
+ /**
32
+
33
+ * index.php
34
+
35
+ *
36
+
37
+ * @since 2018/09/21
38
+
39
+ */
40
+
41
+ function getCalendar(int $year, int $month)
42
+
43
+ {
44
+
45
+ $start = new DateTime();
46
+
47
+ $start->setDate($year, $month, 1);
48
+
49
+ $start->setTime(0, 0, 0);
50
+
51
+ $w = $start->format('w');
52
+
53
+ $start->sub(new DateInterval(sprintf('P%dD', $w)));
54
+
55
+
56
+
57
+ $end = new DateTime();
58
+
59
+ $end->setDate($year, $month + 1, 0);
60
+
61
+ $end->setTime(0, 0, 0);
62
+
63
+ $end->add(new DateInterval(sprintf('P%dD', 7 - $end->format('w'))));
64
+
65
+
66
+
67
+ $arr = [];
68
+
69
+ while ($start < $end) {
70
+
71
+ $arr[] = [
72
+
73
+ 'date' => $start->format('Y-m-d')
74
+
75
+ , 'y' => $start->format('Y')
76
+
77
+ , 'm' => $start->format('n')
78
+
79
+ , 'd' => $start->format('j')
80
+
81
+ , 'week' => $start->format('w')
82
+
83
+ ];
84
+
85
+ $start->add(new DateInterval('P1D'));
86
+
87
+ }
88
+
89
+ $prev = (new DateTime())->setDate($year, $month - 1, 1);
90
+
91
+ $next = (new DateTime())->setDate($year, $month + 1, 1);
92
+
93
+ return [
94
+
95
+ 'prev' => sprintf('?y=%d&m=%d', $prev->format('Y'), $prev->format('m'))
96
+
97
+ , 'next' => sprintf('?y=%d&m=%d', $next->format('Y'), $next->format('m'))
98
+
99
+ , 'calendar' => $arr
100
+
101
+ ];
102
+
103
+ }
104
+
105
+
106
+
107
+ $year = filter_input(INPUT_GET, 'y') ?? (new DateTime())->format('Y');
108
+
109
+ $month = filter_input(INPUT_GET, 'm') ?? (new DateTime())->format('n');
110
+
111
+ $calendar = getCalendar($year, $month);
112
+
113
+ ?>
114
+
115
+ <!DOCTYPE HTML>
116
+
117
+ <html lang="ja">
118
+
119
+ <head>
120
+
121
+ <meta charset="UTF-8">
122
+
123
+ <title>カレンダー</title>
124
+
125
+ <style type="text/css">
126
+
127
+ .calendar {
128
+
129
+ width: 300px;
130
+
131
+ margin: 0 auto 2em;
132
+
133
+ }
134
+
135
+ .calendar td {
136
+
137
+ text-align: center;
138
+
139
+ }
140
+
141
+ </style>
142
+
143
+ </head>
144
+
145
+ <body>
146
+
147
+ <div class="calendar">
148
+
149
+ <table>
150
+
151
+ <thead>
152
+
153
+ <tr>
154
+
155
+ <th>
156
+
157
+ <a href="<?php echo $calendar['prev'] ?>">&laquo;</a>
158
+
159
+ </th>
160
+
161
+ <th colspan="5">
162
+
163
+ <?php printf('%d/%d', $year, $month); ?>
164
+
165
+ </th>
166
+
167
+ <th>
168
+
169
+ <a href="<?php echo $calendar['next'] ?>">&raquo;</a>
170
+
171
+ </th>
172
+
173
+ </tr>
174
+
175
+ <tr>
176
+
177
+ <th>日</th>
178
+
179
+ <th>月</th>
180
+
181
+ <th>火</th>
182
+
183
+ <th>水</th>
184
+
185
+ <th>木</th>
186
+
187
+ <th>金</th>
188
+
189
+ <th>土</th>
190
+
191
+ </tr>
192
+
193
+ </thead
194
+
195
+ <tbody>
196
+
197
+ <?php foreach ($calendar['calendar'] as $dt) : ?>
198
+
199
+
200
+
201
+ <?php if ($dt['week'] == 0) : ?>
202
+
203
+ <tr>
204
+
205
+ <?php endif; ?>
206
+
207
+
208
+
209
+ <td>
210
+
211
+ <a href="date.php?date=<?php echo $dt['date']; ?>">
212
+
213
+ <?php echo $dt['d']; ?>
214
+
215
+ </a>
216
+
217
+ </td>
218
+
219
+
220
+
221
+ <?php if ($dt['week'] == 6) : ?>
222
+
223
+ </tr>
224
+
225
+ <?php endif; ?>
226
+
227
+
228
+
229
+ <?php endforeach; ?>
230
+
231
+ </tbody>
232
+
233
+ </table>
234
+
235
+ </div>
236
+
237
+
238
+
239
+ <div class="calendar">
240
+
241
+ <form action="date.php" method="get">
242
+
243
+ <table>
244
+
245
+ <thead>
246
+
247
+ <tr>
248
+
249
+ <th>
250
+
251
+ <a href="<?php echo $calendar['prev'] ?>">&laquo;</a>
252
+
253
+ </th>
254
+
255
+ <th colspan="5">
256
+
257
+ <?php printf('%d/%d', $year, $month); ?>
258
+
259
+ </th>
260
+
261
+ <th>
262
+
263
+ <a href="<?php echo $calendar['next'] ?>">&raquo;</a>
264
+
265
+ </th>
266
+
267
+ </tr>
268
+
269
+ <tr>
270
+
271
+ <th>日</th>
272
+
273
+ <th>月</th>
274
+
275
+ <th>火</th>
276
+
277
+ <th>水</th>
278
+
279
+ <th>木</th>
280
+
281
+ <th>金</th>
282
+
283
+ <th>土</th>
284
+
285
+ </tr>
286
+
287
+ </thead
288
+
289
+ <tbody>
290
+
291
+ <?php foreach ($calendar['calendar'] as $dt) : ?>
292
+
293
+
294
+
295
+ <?php if ($dt['week'] == 0) : ?>
296
+
297
+ <tr>
298
+
299
+ <?php endif; ?>
300
+
301
+
302
+
303
+ <td>
304
+
305
+ <button name="date" value="<?php echo $dt['date']; ?>">
306
+
307
+ <?php echo $dt['d']; ?>
308
+
309
+ </button>
310
+
311
+ </td>
312
+
313
+
314
+
315
+ <?php if ($dt['week'] == 6) : ?>
316
+
317
+ </tr>
318
+
319
+ <?php endif; ?>
320
+
321
+
322
+
323
+ <?php endforeach; ?>
324
+
325
+ </tbody>
326
+
327
+ </table>
328
+
329
+ </form>
330
+
331
+ </div>
332
+
333
+ </body>
334
+
335
+ </html>
336
+
337
+ ```