Skip to content

Commit

Permalink
from mysql date time
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed Sep 20, 2019
1 parent 986cbe3 commit 4b591ca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
31 changes: 22 additions & 9 deletions src/Util/DateTimeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

namespace doganoo\PHPUtil\Util;

use DateTime;
use Exception;

/**
* Class DateTimeUtil
*
Expand All @@ -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.
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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);
}
}
30 changes: 29 additions & 1 deletion test/Util/DateTimeUtilTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
<?php

/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <[email protected]>
*
* 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;
Expand All @@ -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);
}

}

0 comments on commit 4b591ca

Please sign in to comment.