25
25
26
26
namespace doganoo \PHPUtil \Util ;
27
27
28
+ use DateTime ;
29
+ use Exception ;
30
+
28
31
/**
29
32
* Class DateTimeUtil
30
33
*
@@ -34,6 +37,9 @@ final class DateTimeUtil {
34
37
/** @var string $GERMAN_DATE_TIME_FORMAT */
35
38
public const GERMAN_DATE_TIME_FORMAT = "d.m.Y H:i:s " ;
36
39
40
+ /** @var string $MYSQL_DATE_TIME_FORMAT */
41
+ public const MYSQL_DATE_TIME_FORMAT = "Y-m-d H:i:s " ;
42
+
37
43
/**
38
44
* prevent from instantiation
39
45
* StringUtil constructor.
@@ -43,20 +49,20 @@ private function __construct() {
43
49
44
50
/**
45
51
* @return int
46
- * @throws \ Exception
52
+ * @throws Exception
47
53
*/
48
54
public static function getUnixTimestamp (): int {
49
- return (new \ DateTime ())->getTimestamp ();
55
+ return (new DateTime ())->getTimestamp ();
50
56
}
51
57
52
58
/**
53
59
* @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
57
63
*/
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 ();
60
66
$ dateTime ->modify ("- $ hours hours " );
61
67
return $ dateTime ;
62
68
}
@@ -66,10 +72,10 @@ public static function subtractHours(int $hours, \DateTime $dateTime = null): \D
66
72
*
67
73
* @param int $timestamp
68
74
* @return string
69
- * @throws \ Exception
75
+ * @throws Exception
70
76
*/
71
77
public static function timestampToGermanDateFormat (int $ timestamp ): string {
72
- $ dateTime = new \ DateTime ();
78
+ $ dateTime = new DateTime ();
73
79
$ dateTime ->setTimestamp ($ timestamp );
74
80
$ format = $ dateTime ->format (DateTimeUtil::GERMAN_DATE_TIME_FORMAT );
75
81
return $ format ;
@@ -84,6 +90,13 @@ public static function timestampToGermanDateFormat(int $timestamp): string {
84
90
*/
85
91
public static function valid (string $ date , string $ format ): bool {
86
92
return date ($ format , strtotime ($ date )) === $ date ;
93
+ }
87
94
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 );
88
101
}
89
102
}
0 commit comments