Passed
Push — additional_methods ( ef7aa3...fb96a4 )
by Mark
01:33
created

DateTimeExtended::mysql_minute()   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
nc 2
nop 1
dl 0
loc 7
rs 10
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
            return $asTime->format('G');
88
        }
89
    }
90
91
    // phpcs:disable
92
    public static function mysql_minute($time)
93
    {
94
        // phpcs:enable
95
        if ($time) {
96
            $asTime = new DateTime($time);
97
98
            return date_format($asTime, 'i');
99
        }
100
    }
101
102
    // phpcs:disable
103
    public static function mysql_timediff($timeExpression1, $timeExpression2)
104
    {
105
        // phpcs:enable
106
        if ($timeExpression1 && $timeExpression2) {
107
            $dateTime1 = new DateTime($timeExpression1);
108
            $dateTime2 = new DateTime($timeExpression2);
109
            $dateTimeInterval = $dateTime2->diff($dateTime1);
110
            $days = $dateTimeInterval->d;
111
            $hours = ($days * 24) + $dateTimeInterval->h;
112
            $hourFormatter = new \NumberFormatter(\Locale::DEFAULT_LOCALE, \NumberFormatter::PATTERN_DECIMAL, '00');
113
            $hours = $hourFormatter->format($hours, \NumberFormatter::PATTERN_DECIMAL);
114
115
            return $dateTimeInterval->format("%r$hours:%I:%S.%F");
116
        }
117
    }
118
119
    // phpcs:disable
120
    public static function mysql_timestampdiff($timeUnit, $startTimeStamp, $endTimeStamp)
121
    {
122
        //phpcs:enable
123
        if ($startTimeStamp != null && is_numeric($startTimeStamp) && $endTimeStamp != null && is_numeric($endTimeStamp)) {
124
            $differenceInt = $endTimeStamp - $startTimeStamp;
125
            if ($timeUnit == SubstitutionConstants::SECOND || $timeUnit = SubstitutionConstants::FRAC_SECOND) {
126
                return $differenceInt;
127
            }
128
            $difference = new DateTime();
129
            $difference->setTimestamp($differenceInt);
130
131
            return $difference->format("P$timeUnit");
132
        }
133
    }
134
135
    //phpcs:disable
136
    public static function mysql_time_to_sec($timeExpression)
137
    {
138
        //phpcs:enable
139
        if ($timeExpression != null) {
140
            if (is_numeric($timeExpression)) {
141
                return $timeExpression;
142
            }
143
            $time = new DateTime($timeExpression);
144
            //Convert to the year zero according to Unix Timestamps.
145
            $time->setDate(1970, 1, 1);
146
147
            return $time->getTimestamp();
148
        }
149
    }
150
151
    //phpcs:disable
152
    public static function mysql_utc_timestamp()
153
    {
154
        //phpcs:enable
155
        $now = new DateTime();
156
157
        return $now->getTimestamp();
158
    }
159
160
    // phpcs:disable
161
    public static function mysql_weekday($date)
162
    {
163
        if ($date) {
164
            $dateTime = new DateTime($date);
165
166
            return $dateTime->format('N') - 1;
167
        }
168
    }
169
}
170