Skip to content

Commit

Permalink
make from mysql date time nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Oct 3, 2019
1 parent 7e51e01 commit 3886e1a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Util/DateTimeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @package doganoo\PHPUtil\Util
*/
final class DateTimeUtil {

/** @var string $GERMAN_DATE_TIME_FORMAT */
public const GERMAN_DATE_TIME_FORMAT = "d.m.Y H:i:s";

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

/**
* @param int $hours
* @param int $hours
* @param DateTime|null $dateTime
* @return DateTime
* @throws Exception
Expand Down Expand Up @@ -93,19 +94,21 @@ public static function valid(string $date, string $format): bool {
}

/**
* @param string $date
* @return DateTime
* @param string $date|null
* @return DateTime|null
*/
public static function fromMysqlDateTime(string $date): DateTime{
public static function fromMysqlDateTime(?string $date): ?DateTime {
if (null === $date) return null;
return DateTime::createFromFormat(DateTimeUtil::MYSQL_DATE_TIME_FORMAT, $date);
}

/**
* @param DateTime|null $dateTime
* @return string|null
*/
public static function formatMysqlDateTime(?DateTime $dateTime):?string {
public static function formatMysqlDateTime(?DateTime $dateTime): ?string {
if (null === $dateTime) return null;
return $dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT);
}

}
1 change: 1 addition & 0 deletions test/Util/DateTimeUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testValid(){
}

public function testFromMysqlDateTime(){
$this->assertNull($dateTime = DateTimeUtil::fromMysqlDateTime(null));
$dateTimeString = "2019-09-20 19:21:47";
$dateTime = DateTimeUtil::fromMysqlDateTime($dateTimeString);
$this->assertTrue($dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT) === $dateTimeString);
Expand Down

0 comments on commit 3886e1a

Please sign in to comment.