Completed
Push — additional_methods ( 3aae86...4c7a76 )
by Mark
04:12 queued 11s
created

DateTimeExtended::mysql_convert_tz()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 3
dl 0
loc 14
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Mhorninger\MySQLite\MySQL;
4
5
use DateTime;
6
use DateTimeZone;
7
use Mhorninger\MySQLite\SubstitutionConstants;
0 ignored issues
show
Bug introduced by
The type Mhorninger\MySQLite\SubstitutionConstants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
trait DateTimeExtended
10
{
11
    //phpcs:disable
12
    public static function mysql_convert_tz($date, $fromTimezone, $toTimezone)
13
    {
14
        //phpcs:enable
15
        if ($date && $fromTimezone && $toTimezone) {
16
            $toTimezone = new DateTimeZone($toTimezone);
17
            if ($fromTimezone == 'SYSTEM') {
18
                $converted = new DateTime($date);
19
            } else {
20
                $fromTimezone = new DateTimeZone($fromTimezone);
21
                $converted = new DateTime($date, $fromTimezone);
22
            }
23
            $converted->setTimezone($toTimezone);
24
25
            return $converted->format('Y-m-d H:i:s');
26
        }
27
    }
28
29
    //phpcs:disable
30
    public static function mysql_date_format($date, $format)
31
    {
32
        //phpcs:enable
33
        $dictionary = [
34
            '%a' => 'D',
35
            '%b' => 'M',
36
            '%c' => 'n',
37
            '%D' => 'jS',
38
            '%d' => 'd',
39
            '%e' => 'j',
40
            '%f' => 'u',
41
            '%H' => 'H',
42
            '%h' => 'h',
43
            '%I' => 'h',
44
            '%i' => 'i',
45
            '%j' => 'z',
46
            '%k' => 'G',
47
            '%l' => 'g',
48
            '%M' => 'F',
49
            '%m' => 'm',
50
            '%p' => 'A',
51
            '%r' => 'h:i:s A',
52
            '%S' => 's',
53
            '%s' => 's',
54
            '%T' => 'H:i:s',
55
            '%u' => 'W',
56
            '%v' => 'W',
57
            '%W' => 'l',
58
            '%w' => 'w',
59
            '%x' => 'o',
60
            '%Y' => 'Y',
61
            '%y' => 'y',
62
            '%%' => '%',
63
        ];
64
65
        if ($date && $format) {
66
            $time = new DateTime($date);
67
            $keys = array_keys($dictionary);
68
            foreach ($keys as $key) {
69
                $format = str_replace($key, $dictionary[$key], $format);
70
            }
71
72
            return $time->format($format);
73
        }
74
    }
75
76
    // phpcs:disable
77
    public static function mysql_minute($time)
78
    {
79
        // phpcs:enable
80
        if ($time) {
81
            $asTime = new DateTime($time);
82
83
            return date_format($asTime, 'i');
84
        }
85
    }
86
87
    // phpcs:disable
88
    public static function mysql_timediff($timeExpression1, $timeExpression2)
89
    {
90
        // phpcs:enable
91
        if ($timeExpression1 && $timeExpression2) {
92
            $dateTime1 = new DateTime($timeExpression1);
93
            $dateTime2 = new DateTime($timeExpression2);
94
            $dateTimeInterval = $dateTime2->diff($dateTime1);
95
            $days = $dateTimeInterval->d;
96
            $hours = ($days * 24) + $dateTimeInterval->h;
97
            $hourFormatter = new \NumberFormatter(\Locale::DEFAULT_LOCALE, \NumberFormatter::PATTERN_DECIMAL, '00');
98
            $hours = $hourFormatter->format($hours, \NumberFormatter::PATTERN_DECIMAL);
99
100
            return $dateTimeInterval->format("%r$hours:%I:%S.%F");
101
        }
102
    }
103
104
    // phpcs:disable
105
    public static function mysql_timestampdiff($timeUnit, $startTimeStamp, $endTimeStamp)
106
    {
107
        //phpcs:enable
108
        if ($startTimeStamp != null && is_numeric($startTimeStamp) && $endTimeStamp != null && is_numeric($endTimeStamp)) {
109
            $differenceInt = $endTimeStamp - $startTimeStamp;
110
            if ($timeUnit == SubstitutionConstants::SECOND || $timeUnit = SubstitutionConstants::FRAC_SECOND) {
111
                return $differenceInt;
112
            }
113
            $difference = new DateTime();
114
            $difference->setTimestamp($differenceInt);
115
116
            return $difference->format("P$timeUnit");
117
        }
118
    }
119
120
    //phpcs:disable
121
    public static function mysql_time_to_sec($timeExpression)
122
    {
123
        //phpcs:enable
124
        if ($timeExpression != null) {
125
            if (is_numeric($timeExpression)) {
126
                return $timeExpression;
127
            }
128
            $time = new DateTime($timeExpression);
129
            //Convert to the year zero according to Unix Timestamps.
130
            $time->setDate(1970, 1, 1);
131
132
            return $time->getTimestamp();
133
        }
134
    }
135
136
    //phpcs:disable
137
    public static function mysql_utc_timestamp()
138
    {
139
        //phpcs:enable
140
        $now = new DateTime();
141
142
        return $now->getTimestamp();
143
    }
144
145
    // phpcs:disable
146
    public static function mysql_weekday($date)
147
    {
148
        if ($date) {
149
            $dateTime = new DateTime($date);
150
151
            return $dateTime->format('N') - 1;
152
        }
153
    }
154
}
155