回答編集履歴

1

修正

2019/08/09 12:38

投稿

m.ts10806
m.ts10806

スコア80852

test CHANGED
@@ -19,3 +19,253 @@
19
19
 
20
20
 
21
21
  `>`で遷移した時のURL見てみると原因わかりますよ。
22
+
23
+
24
+
25
+ 追記:
26
+
27
+ コードインデントはつけられたほうが読みやすいです。
28
+
29
+ コードフォーマット機能のついたエディタ、できればIDE導入を強くすすめます。
30
+
31
+
32
+
33
+ 以下はEclipseのコードフォーマットの結果
34
+
35
+ ```php
36
+
37
+ <?php
38
+
39
+ date_default_timezone_set('Asia/Tokyo');
40
+
41
+
42
+
43
+ if (isset($_GET['ym'])) {
44
+
45
+ $ym = $_GET['ym'];
46
+
47
+ } else {
48
+
49
+
50
+
51
+ $ym = date('Y-m');
52
+
53
+ }
54
+
55
+
56
+
57
+ $timestamp = strtotime($ym . '-01');
58
+
59
+ if ($timestamp === false) {
60
+
61
+ $ym = date('Y-m');
62
+
63
+ $timestamp = strtotime($ym . '-01');
64
+
65
+ }
66
+
67
+
68
+
69
+ $today = date('Y-m-j', time());
70
+
71
+
72
+
73
+ $html_title = date('Y年n月', $timestamp);
74
+
75
+
76
+
77
+ $prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp) - 1, 1, date('Y', $timestamp)));
78
+
79
+ $next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp) + 1, 1, date('Y', $timestamp)));
80
+
81
+
82
+
83
+ $day_count = date('t', $timestamp);
84
+
85
+ $youbi = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
86
+
87
+ $weeks = [];
88
+
89
+ $week = '';
90
+
91
+ $week .= str_repeat('<td></td>', $youbi);
92
+
93
+
94
+
95
+ for ($day = 1; $day <= $day_count; $day ++, $youbi ++) {
96
+
97
+ $date = $ym . '-' . $day;
98
+
99
+ if ($today == $date) {
100
+
101
+ $week .= '<td class="today">' . $day;
102
+
103
+ } else {
104
+
105
+ $week .= '<td>' . $day;
106
+
107
+ }
108
+
109
+ $week .= '</td>';
110
+
111
+ if ($youbi % 7 == 6 || $day == $day_count) {
112
+
113
+ if ($day == $day_count) {
114
+
115
+ $week .= str_repeat('<td></td>', 6 - ($youbi % 7));
116
+
117
+ }
118
+
119
+ $weeks[] = '<tr>' . $week . '</tr>';
120
+
121
+ $week = '';
122
+
123
+ }
124
+
125
+ }
126
+
127
+ ?>
128
+
129
+ <!DOCTYPE html>
130
+
131
+ <html lang='ja'>
132
+
133
+ <head>
134
+
135
+ <meta charset='utf-8'>
136
+
137
+ <title>PHPカレンダー</title>
138
+
139
+ <link rel="stylesheet"
140
+
141
+ href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
142
+
143
+ integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
144
+
145
+ crossorigin="anonymous">
146
+
147
+
148
+
149
+ <link
150
+
151
+ href="https://fonts.googleapis.com/css?family=Noto+Sans+JP&display=swap"
152
+
153
+ rel="stylesheet">
154
+
155
+ <style>
156
+
157
+ .container {
158
+
159
+ font-family: 'Noto Sans JP', sans-serif;
160
+
161
+ margin-top: 80px;
162
+
163
+ }
164
+
165
+
166
+
167
+ h3 {
168
+
169
+ margin-bottom: 30px;
170
+
171
+ }
172
+
173
+
174
+
175
+ th {
176
+
177
+ height: 30px;
178
+
179
+ text-align: center;
180
+
181
+ }
182
+
183
+
184
+
185
+ td {
186
+
187
+ height: 30px;
188
+
189
+ }
190
+
191
+
192
+
193
+ .today {
194
+
195
+ background: green;
196
+
197
+ }
198
+
199
+
200
+
201
+ th:nth-of-type(1), td:nth-of-type(1) {
202
+
203
+ color: red;
204
+
205
+ }
206
+
207
+
208
+
209
+ th:nth-of-type(7), td:nth-of-type(7) {
210
+
211
+ color: blue;
212
+
213
+ }
214
+
215
+ </style>
216
+
217
+ </head>
218
+
219
+ <body>
220
+
221
+ <div class='container'>
222
+
223
+ <h3>
224
+
225
+ <a href="?ym=<?php echo $prev; ?>">&lt;</a> <?php echo $html_title; ?> <a
226
+
227
+ href="?ym=<?php echo $next; ?>">&gt;</a>
228
+
229
+ </h3>
230
+
231
+ <table class="table table-bordered">
232
+
233
+ <tr>
234
+
235
+ <th>日</th>
236
+
237
+ <th>月</th>
238
+
239
+ <th>火</th>
240
+
241
+ <th>水</th>
242
+
243
+ <th>木</th>
244
+
245
+ <th>金</th>
246
+
247
+ <th>土</th>
248
+
249
+
250
+
251
+ </tr>
252
+
253
+ <?php
254
+
255
+ foreach($weeks as $week){
256
+
257
+ echo $week;
258
+
259
+ }
260
+
261
+ ?>
262
+
263
+ </table>
264
+
265
+ </div>
266
+
267
+ </body>
268
+
269
+ </html>
270
+
271
+ ```