Passed
Push — master ( 2295a4...eec722 )
by Peter
10:03
created

DateHelper::mysqlDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Helper;
6
7
use AbterPhp\Framework\Constant\Env;
8
use Opulence\Environments\Environment;
9
10
class DateHelper
11
{
12
    const MYSQL_DATE_FORMAT     = "Y-m-d";
13
    const MYSQL_DATETIME_FORMAT = "Y-m-d H:i:s";
14
15
    /**
16
     * @param \DateTime|null $dateTime
17
     *
18
     * @return string
19
     */
20
    public static function format(?\DateTime $dateTime): string
21
    {
22
        if (!$dateTime) {
23
            return '';
24
        }
25
26
        return $dateTime->format(Environment::getVar(Env::ADMIN_DATE_FORMAT));
27
    }
28
29
    /**
30
     * @param \DateTime|null $dateTime
31
     *
32
     * @return string
33
     */
34
    public static function formatDateTime(?\DateTime $dateTime): string
35
    {
36
        if (!$dateTime) {
37
            return '';
38
        }
39
40
        return $dateTime->format(Environment::getVar(Env::ADMIN_DATETIME_FORMAT));
41
    }
42
43
    /**
44
     * @param \DateTime|null $date
45
     *
46
     * @return string
47
     */
48
    public static function mysqlDate(?\DateTime $date = null): string
49
    {
50
        if (!$date) {
51
            $date = new \DateTime();
52
        }
53
54
        return $date->format(static::MYSQL_DATE_FORMAT);
55
    }
56
57
    /**
58
     * @param \DateTime|null $dateTime
59
     *
60
     * @return string
61
     */
62
    public static function mysqlDateTime(?\DateTime $dateTime = null): string
63
    {
64
        if (!$dateTime) {
65
            $dateTime = new \DateTime();
66
        }
67
68
        return $dateTime->format(static::MYSQL_DATETIME_FORMAT);
69
    }
70
}
71