質問するログイン新規登録

回答編集履歴

1

修正

2017/03/29 16:54

投稿

退会済みユーザー
answer CHANGED
@@ -8,24 +8,18 @@
8
8
  function check_month($date)
9
9
  {
10
10
  $now = new DateTime();
11
+ $now_month = $now->format('Y') * 12 + $now->format('n');
11
12
  $dt = new DateTime($date);
12
- $diff = (int) $dt->format('Ym') - (int) $now->format('Ym');
13
+ $dt_month = $dt->format('Y') * 12 + $dt->format('n');
13
14
 
14
- switch ($diff) {
15
- case 0:
16
- return '今月';
15
+ $diff = $dt_month - $now_month;
17
- case 1;
18
- return '来月';
19
- case 2:
20
- return '再来月';
16
+ return (boolean) (0 <= $diff && $diff < 3);
21
- default :
22
- return null;
23
- }
24
17
  }
25
18
 
26
- echo check_month('2017-01-18'); // null
19
+ var_dump(check_month('2017-02-18')); // false
20
+ var_dump(check_month('2017-03-18')); // true
27
- echo check_month('2017-04-18'); // 来月
21
+ var_dump(check_month('2017-04-18')); // true
28
- echo check_month('2017-05-18'); // 再来月
22
+ var_dump(check_month('2017-05-18')); // true
29
- echo check_month('2017-06-18'); // null
23
+ var_dump(check_month('2017-06-18')); // false
30
24
 
31
25
  ```