DateTimeExtended::mysql_convert_tz()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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