DateHelper   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hasTimeReached() 0 3 1
A getMysqlDateTimeFromDateTime6() 0 2 1
A getDatetime6() 0 8 1
A getDatetimeForDisplay() 0 2 1
A getDateDifferenceInDays() 0 5 1
A getEndOfTime() 0 2 1
A __construct() 0 4 1
1
<?php
2
namespace andmemasin\helpers;
3
4
use \DateTime;
5
6
/**
7
 * A helper class to contain general datetime helper functions
8
 *
9
 * @package app\models\helpers
10
 * @author Tonis Ormisson <[email protected]>
11
 */
12
class DateHelper {
13
14
    /** @var string End of time for DB logical delete. */
15
    const END_OF_TIME = '3000-12-31 00:00:00.000000';
16
17
    /** @var DateTime */
18
    public $now;
19
20
21
    public function __construct()
22
    {
23
        $timeZone = new \DateTimeZone(\Yii::$app->timeZone);
24
        $this->now = new DateTime('now', $timeZone);
25
    }
26
27
28
    /**
29
     * Get current time in mysql datetime(6) format as "Y-m-d H:i:s.u"
30
     * @return string
31
     */
32
    public function getDatetime6() {
33
34
        // TODO this does now consider the getNow()
35
        $t = microtime(true);
36
        $micro = sprintf("%06d", ($t - floor($t)) * 1000000);
37
        $d = new DateTime(date('Y-m-d H:i:s.'.$micro, $t));
38
39
        return $d->format("Y-m-d H:i:s.u"); // note at point on "u"
40
41
    }
42
43
    /**
44
     * End of time is a default value for time not in reach to mark
45
     * date where we will not reach. Typically default for time_closed value
46
     * @return string
47
     */
48
    public function getEndOfTime() {
49
        return self::END_OF_TIME;
50
    }
51
52
53
    /**
54
     * @param string $datetime
55
     * @return false|string
56
     */
57
    public function getMysqlDateTimeFromDateTime6($datetime) {
58
        return date("Y-m-d H:i:s", strtotime($datetime));
59
    }
60
61
62
    /**
63
     * @param string  $dateTime
64
     * @return false|string
65
     * @deprecated use Formatter
66
     */
67
    public function getDatetimeForDisplay($dateTime) {
68
        return date("Y-m-d H:i:s", strtotime($dateTime));
69
    }
70
71
    /**
72
     * @param string $sqlDate
73
     * @return mixed
74
     */
75
    public function getDateDifferenceInDays($sqlDate) {
76
        $dStart = $this->now;
77
        $dEnd = new DateTime($sqlDate);
78
        $dDiff = $dStart->diff($dEnd);
79
        return (int) $dDiff->format('%r%a');
80
    }
81
82
    /**
83
     * @param string $dateTime time that we want to check whether it has reached or not
84
     * @return bool
85
     */
86
    public function hasTimeReached($dateTime)
87
    {
88
        return $this->now >= new \DateTime($dateTime);
89
    }
90
91
}
92