Skip to content

Commit 4b591ca

Browse files
committed
from mysql date time
1 parent 986cbe3 commit 4b591ca

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

src/Util/DateTimeUtil.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
namespace doganoo\PHPUtil\Util;
2727

28+
use DateTime;
29+
use Exception;
30+
2831
/**
2932
* Class DateTimeUtil
3033
*
@@ -34,6 +37,9 @@ final class DateTimeUtil {
3437
/** @var string $GERMAN_DATE_TIME_FORMAT */
3538
public const GERMAN_DATE_TIME_FORMAT = "d.m.Y H:i:s";
3639

40+
/** @var string $MYSQL_DATE_TIME_FORMAT */
41+
public const MYSQL_DATE_TIME_FORMAT = "Y-m-d H:i:s";
42+
3743
/**
3844
* prevent from instantiation
3945
* StringUtil constructor.
@@ -43,20 +49,20 @@ private function __construct() {
4349

4450
/**
4551
* @return int
46-
* @throws \Exception
52+
* @throws Exception
4753
*/
4854
public static function getUnixTimestamp(): int {
49-
return (new \DateTime())->getTimestamp();
55+
return (new DateTime())->getTimestamp();
5056
}
5157

5258
/**
5359
* @param int $hours
54-
* @param \DateTime|null $dateTime
55-
* @return \DateTime
56-
* @throws \Exception
60+
* @param DateTime|null $dateTime
61+
* @return DateTime
62+
* @throws Exception
5763
*/
58-
public static function subtractHours(int $hours, \DateTime $dateTime = null): \DateTime {
59-
if (null === $dateTime) $dateTime = new \DateTime();
64+
public static function subtractHours(int $hours, DateTime $dateTime = null): DateTime {
65+
if (null === $dateTime) $dateTime = new DateTime();
6066
$dateTime->modify("-$hours hours");
6167
return $dateTime;
6268
}
@@ -66,10 +72,10 @@ public static function subtractHours(int $hours, \DateTime $dateTime = null): \D
6672
*
6773
* @param int $timestamp
6874
* @return string
69-
* @throws \Exception
75+
* @throws Exception
7076
*/
7177
public static function timestampToGermanDateFormat(int $timestamp): string {
72-
$dateTime = new \DateTime();
78+
$dateTime = new DateTime();
7379
$dateTime->setTimestamp($timestamp);
7480
$format = $dateTime->format(DateTimeUtil::GERMAN_DATE_TIME_FORMAT);
7581
return $format;
@@ -84,6 +90,13 @@ public static function timestampToGermanDateFormat(int $timestamp): string {
8490
*/
8591
public static function valid(string $date, string $format): bool {
8692
return date($format, strtotime($date)) === $date;
93+
}
8794

95+
/**
96+
* @param string $date
97+
* @return DateTime
98+
*/
99+
public static function fromMysqlDateTime(string $date): DateTime{
100+
return DateTime::createFromFormat(DateTimeUtil::MYSQL_DATE_TIME_FORMAT, $date);
88101
}
89102
}

test/Util/DateTimeUtilTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
<?php
2-
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
325

426
use doganoo\PHPUtil\Util\DateTimeUtil;
527
use PHPUnit\Framework\TestCase;
@@ -15,4 +37,10 @@ public function testValid(){
1537
$this->assertTrue(true === DateTimeUtil::valid("01-02-2018", "d-m-Y"));
1638
}
1739

40+
public function testFromMysqlDateTime(){
41+
$dateTimeString = "2019-09-20 19:21:47";
42+
$dateTime = DateTimeUtil::fromMysqlDateTime($dateTimeString);
43+
$this->assertTrue($dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT) === $dateTimeString);
44+
}
45+
1846
}

0 commit comments

Comments
 (0)