File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -74,4 +74,16 @@ public static function timestampToGermanDateFormat(int $timestamp): string {
74
74
$ format = $ dateTime ->format (DateTimeUtil::GERMAN_DATE_TIME_FORMAT );
75
75
return $ format ;
76
76
}
77
+
78
+ /**
79
+ * Whether string is a valid date or not
80
+ *
81
+ * @param string $date
82
+ * @param string $format
83
+ * @return bool
84
+ */
85
+ public static function valid (string $ date , string $ format ): bool {
86
+ return date ($ format , strtotime ($ date )) === $ date ;
87
+
88
+ }
77
89
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+
4
+ use doganoo \PHPUtil \Util \DateTimeUtil ;
5
+ use PHPUnit \Framework \TestCase ;
6
+
7
+ class DateTimeUtilTest extends TestCase {
8
+
9
+ public function testValid (){
10
+
11
+ $ this ->assertTrue (true === DateTimeUtil::valid ("2019-01-01 " , "Y-m-d " ));
12
+ $ this ->assertTrue (false === DateTimeUtil::valid ("sfsfsdfs " , "Y-m-d " ));
13
+ $ this ->assertTrue (false === DateTimeUtil::valid ("-1 " , "y-m-d " ));
14
+ $ this ->assertTrue (false === DateTimeUtil::valid ("2019-02-30 " , "Y-m-d " ));
15
+ $ this ->assertTrue (true === DateTimeUtil::valid ("01-02-2018 " , "d-m-Y " ));
16
+ }
17
+
18
+ }
You can’t perform that action at this time.
0 commit comments