回答編集履歴

1

追記

2017/02/16 09:13

投稿

yambejp
yambejp

スコア114839

test CHANGED
@@ -29,3 +29,39 @@
29
29
 
30
30
 
31
31
  ```
32
+
33
+ # 追記
34
+
35
+ 例えばこうすると年月だけ拾えます
36
+
37
+ ```PHP
38
+
39
+ print normalizeTime("2017年2月16日")."<br>";// y/m/d
40
+
41
+ print normalizeTime("2017年2月")."<br>";// y/m
42
+
43
+ print normalizeTime("2017年2月99日")."<br>";// ありえない日付なので不明
44
+
45
+ print normalizeTime("2017年2月160日")."<br>"; // 日付が3桁なので日付を無視して y/m
46
+
47
+
48
+
49
+ function normalizeTime($date_str) {
50
+
51
+ $pattern="/(\d{4})年(\d{1,2})月(?:(\d{1,2})(?=日))?/";
52
+
53
+ if(preg_match($pattern,$date_str,$match) and checkdate($match[2],isset($match[3])?$match[3]:1,$match[1])){
54
+
55
+ return date(isset($match[3])?"Y/m/d":"Y/m",mktime(0,0,0,$match[2],isset($match[3])?$match[3]:1,$match[1]));
56
+
57
+ }else{
58
+
59
+ return "不明";
60
+
61
+ }
62
+
63
+ }
64
+
65
+
66
+
67
+ ```