Skip to content

Commit e39bf11

Browse files
author
Dogan Ucar
committed
valid datetime
1 parent 4bc8441 commit e39bf11

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Util/DateTimeUtil.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ public static function timestampToGermanDateFormat(int $timestamp): string {
7474
$format = $dateTime->format(DateTimeUtil::GERMAN_DATE_TIME_FORMAT);
7575
return $format;
7676
}
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+
}
7789
}

test/Util/DateTimeUtilTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)