Skip to content

Commit 7e51e01

Browse files
committed
format mysql date time
1 parent 4b591ca commit 7e51e01

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.editorconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
root = true
2+
3+
[*]
4+
ij_php_block_brace_style = end_of_line
5+
ij_php_class_brace_style = end_of_line
6+
ij_php_for_brace_force = always
7+
ij_php_if_brace_force = if_multiline
8+
ij_php_method_brace_style = end_of_line
9+
ij_php_space_after_type_cast = true
10+
ij_php_space_after_colon_in_return_type = true
11+
ij_php_blank_lines_after_imports = 1
12+
ij_php_blank_lines_before_imports = 1
13+
ij_php_align_class_constants = true
14+
ij_php_align_assignments = true
15+
ij_php_align_inline_comments = true
16+
ij_php_align_key_value_pairs = true
17+
ij_php_align_group_field_declarations = true
18+
ij_php_align_multiline_array_initializer_expression = true
19+
ij_php_align_multiline_for = true
20+
ij_php_align_phpdoc_comments = true
21+
ij_php_align_phpdoc_param_names = true
22+
ij_php_import_sorting = alphabetic
23+
ij_php_blank_lines_after_class_header = 1
24+
ij_php_blank_lines_before_class_end = 1
25+
ij_php_else_if_style = separate
26+
ij_any_else_on_new_line = false

src/Util/DateTimeUtil.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,13 @@ public static function valid(string $date, string $format): bool {
9999
public static function fromMysqlDateTime(string $date): DateTime{
100100
return DateTime::createFromFormat(DateTimeUtil::MYSQL_DATE_TIME_FORMAT, $date);
101101
}
102+
103+
/**
104+
* @param DateTime|null $dateTime
105+
* @return string|null
106+
*/
107+
public static function formatMysqlDateTime(?DateTime $dateTime):?string {
108+
if (null === $dateTime) return null;
109+
return $dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT);
110+
}
102111
}

test/Util/DateTimeUtilTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ public function testFromMysqlDateTime(){
4343
$this->assertTrue($dateTime->format(DateTimeUtil::MYSQL_DATE_TIME_FORMAT) === $dateTimeString);
4444
}
4545

46+
public function testFormatMysqlDateTime(){
47+
$this->assertNull(DateTimeUtil::formatMysqlDateTime(null));
48+
$dateTimeString = "2019-09-20 19:21:47";
49+
$dateTime = DateTimeUtil::fromMysqlDateTime($dateTimeString);
50+
$formatted = DateTimeUtil::formatMysqlDateTime($dateTime);
51+
$this->assertTrue($formatted === $dateTimeString);
52+
}
53+
4654
}

0 commit comments

Comments
 (0)