回答編集履歴

1

修正

2017/03/29 16:54

投稿

退会済みユーザー
test CHANGED
@@ -18,43 +18,31 @@
18
18
 
19
19
  $now = new DateTime();
20
20
 
21
+ $now_month = $now->format('Y') * 12 + $now->format('n');
22
+
21
23
  $dt = new DateTime($date);
22
24
 
23
- $diff = (int) $dt->format('Ym') - (int) $now->format('Ym');
25
+ $dt_month = $dt->format('Y') * 12 + $dt->format('n');
24
26
 
25
27
 
26
28
 
27
- switch ($diff) {
29
+ $diff = $dt_month - $now_month;
28
30
 
29
- case 0:
30
-
31
- return '今月';
32
-
33
- case 1;
34
-
35
- return '来月';
36
-
37
- case 2:
38
-
39
- return '再来月';
31
+ return (boolean) (0 <= $diff && $diff < 3);
40
-
41
- default :
42
-
43
- return null;
44
-
45
- }
46
32
 
47
33
  }
48
34
 
49
35
 
50
36
 
51
- echo check_month('2017-01-18'); // null
37
+ var_dump(check_month('2017-02-18')); // false
52
38
 
53
- echo check_month('2017-04-18'); // 来月
39
+ var_dump(check_month('2017-03-18')); // true
54
40
 
55
- echo check_month('2017-05-18'); // 再来月
41
+ var_dump(check_month('2017-04-18')); // true
56
42
 
43
+ var_dump(check_month('2017-05-18')); // true
44
+
57
- echo check_month('2017-06-18'); // null
45
+ var_dump(check_month('2017-06-18')); // false
58
46
 
59
47
 
60
48