diff --git a/src/Util/DateTimeUtil.php b/src/Util/DateTimeUtil.php index 16d72b8..80a38d5 100644 --- a/src/Util/DateTimeUtil.php +++ b/src/Util/DateTimeUtil.php @@ -25,6 +25,9 @@ namespace doganoo\PHPUtil\Util; +use DateTime; +use Exception; + /** * Class DateTimeUtil * @@ -34,6 +37,9 @@ final class DateTimeUtil { /** @var string $GERMAN_DATE_TIME_FORMAT */ public const GERMAN_DATE_TIME_FORMAT = "d.m.Y H:i:s"; + /** @var string $MYSQL_DATE_TIME_FORMAT */ + public const MYSQL_DATE_TIME_FORMAT = "Y-m-d H:i:s"; + /** * prevent from instantiation * StringUtil constructor. @@ -43,20 +49,20 @@ private function __construct() { /** * @return int - * @throws \Exception + * @throws Exception */ public static function getUnixTimestamp(): int { - return (new \DateTime())->getTimestamp(); + return (new DateTime())->getTimestamp(); } /** * @param int $hours - * @param \DateTime|null $dateTime - * @return \DateTime - * @throws \Exception + * @param DateTime|null $dateTime + * @return DateTime + * @throws Exception */ - public static function subtractHours(int $hours, \DateTime $dateTime = null): \DateTime { - if (null === $dateTime) $dateTime = new \DateTime(); + public static function subtractHours(int $hours, DateTime $dateTime = null): DateTime { + if (null === $dateTime) $dateTime = new DateTime(); $dateTime->modify("-$hours hours"); return $dateTime; } @@ -66,10 +72,10 @@ public static function subtractHours(int $hours, \DateTime $dateTime = null): \D * * @param int $timestamp * @return string - * @throws \Exception + * @throws Exception */ public static function timestampToGermanDateFormat(int $timestamp): string { - $dateTime = new \DateTime(); + $dateTime = new DateTime(); $dateTime->setTimestamp($timestamp); $format = $dateTime->format(DateTimeUtil::GERMAN_DATE_TIME_FORMAT); return $format; @@ -84,6 +90,13 @@ public static function timestampToGermanDateFormat(int $timestamp): string { */ public static function valid(string $date, string $format): bool { return date($format, strtotime($date)) === $date; + } + /** + * @param string $date + * @return DateTime + */ + public static function fromMysqlDateTime(string $date): DateTime{ + return DateTime::createFromFormat(DateTimeUtil::MYSQL_DATE_TIME_FORMAT, $date); } } \ No newline at end of file diff --git a/test/Util/DateTimeUtilTest.php b/test/Util/DateTimeUtilTest.php index 28a3db9..f09acc7 100644 --- a/test/Util/DateTimeUtilTest.php +++ b/test/Util/DateTimeUtilTest.php @@ -1,5 +1,27 @@ + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ use doganoo\PHPUtil\Util\DateTimeUtil; use PHPUnit\Framework\TestCase; @@ -15,4 +37,10 @@ public function testValid(){ $this->assertTrue(true === DateTimeUtil::valid("01-02-2018", "d-m-Y")); } + public function testFromMysqlDateTime(){ + $dateTimeString = "2019-09-20 19:21:47"; + $dateTime = DateTimeUtil::fromMysqlDateTime($dateTimeString); + $this->assertTrue($dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT) === $dateTimeString); + } + } \ No newline at end of file