Skip to content

Commit 3886e1a

Browse files
committed
make from mysql date time nullable
1 parent 7e51e01 commit 3886e1a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Util/DateTimeUtil.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @package doganoo\PHPUtil\Util
3535
*/
3636
final class DateTimeUtil {
37+
3738
/** @var string $GERMAN_DATE_TIME_FORMAT */
3839
public const GERMAN_DATE_TIME_FORMAT = "d.m.Y H:i:s";
3940

@@ -56,7 +57,7 @@ public static function getUnixTimestamp(): int {
5657
}
5758

5859
/**
59-
* @param int $hours
60+
* @param int $hours
6061
* @param DateTime|null $dateTime
6162
* @return DateTime
6263
* @throws Exception
@@ -93,19 +94,21 @@ public static function valid(string $date, string $format): bool {
9394
}
9495

9596
/**
96-
* @param string $date
97-
* @return DateTime
97+
* @param string $date|null
98+
* @return DateTime|null
9899
*/
99-
public static function fromMysqlDateTime(string $date): DateTime{
100+
public static function fromMysqlDateTime(?string $date): ?DateTime {
101+
if (null === $date) return null;
100102
return DateTime::createFromFormat(DateTimeUtil::MYSQL_DATE_TIME_FORMAT, $date);
101103
}
102104

103105
/**
104106
* @param DateTime|null $dateTime
105107
* @return string|null
106108
*/
107-
public static function formatMysqlDateTime(?DateTime $dateTime):?string {
109+
public static function formatMysqlDateTime(?DateTime $dateTime): ?string {
108110
if (null === $dateTime) return null;
109111
return $dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT);
110112
}
113+
111114
}

test/Util/DateTimeUtilTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function testValid(){
3838
}
3939

4040
public function testFromMysqlDateTime(){
41+
$this->assertNull($dateTime = DateTimeUtil::fromMysqlDateTime(null));
4142
$dateTimeString = "2019-09-20 19:21:47";
4243
$dateTime = DateTimeUtil::fromMysqlDateTime($dateTimeString);
4344
$this->assertTrue($dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT) === $dateTimeString);

0 commit comments

Comments
 (0)