Completed
Push — master ( b362df...addd3e )
by WEBEWEB
02:33
created

DateTimeRenderer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A renderAge() 0 12 2
A renderDateTime() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\CoreBundle\Renderer;
13
14
use DateTime;
15
use Exception;
16
17
/**
18
 * Date/time renderer.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\CoreBundle\Renderer
22
 */
23
class DateTimeRenderer {
24
25
    /**
26
     * Date/time format.
27
     *
28
     * @var string
29
     */
30
    const DATETIME_FORMAT = "Y-m-d H:i";
31
32
    /**
33
     * Render an age.
34
     *
35
     * @param DateTime $birthDate The birth date.
36
     * @param DateTime|null $refDate The reference date.
37
     * @return int Returns the age.
38
     * @throws Exception Throws an exception if an errors occurs.
39
     */
40
    public static function renderAge(DateTime $birthDate, DateTime $refDate = null) {
41
42
        // Use the current date/time.
43
        if (null === $refDate) {
44
            $refDate = new DateTime();
45
        }
46
47
        $diff  = $refDate->getTimestamp() - $birthDate->getTimestamp();
48
        $years = new DateTime("@" . $diff);
49
50
        return intval($years->format("Y")) - 1970;
51
    }
52
53
    /**
54
     * Render a date/time.
55
     *
56
     * @param DateTime|null $dateTime The date/time.
57
     * @param string $format The format.
58
     * @return string Returns the rendered date/time.
59
     */
60
    public static function renderDateTime(DateTime $dateTime = null, $format = self::DATETIME_FORMAT) {
61
        if (null === $dateTime) {
62
            return "";
63
        }
64
        return $dateTime->format($format);
65
    }
66
}
67