Date.php
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @package Regular Labs Library
* @version 18.2.10140
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Library;
defined('_JEXEC') or die;
use DateTimeZone;
use JFactory;
class Date
{
/**
* Convert string to a correct date format ('00-00-00 00:00:00' or '00-00-00') or null
*
* @param string $date
*
* @return null|string
*/
public static function fix($date)
{
if ( ! $date)
{
return null;
}
$date = trim($date);
// Check if date has correct syntax: 00-00-00 00:00:00
// If so, the date format is correct
if ( ! RegEx::match('^[0-9]+-[0-9]+-[0-9]+( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$', $date))
{
return $date;
}
// Check if date has syntax: 00-00-00 00:00
// If so, it is missing the seconds, so add :00 (seconds)
if (RegEx::match('^[0-9]+-[0-9]+-[0-9]+ [0-9][0-9]:[0-9][0-9]$', $date))
{
return $date . ':00';
}
// Check if date has a prepending date syntax: 00-00-00
// If so, it is missing a correct time time, so add 00:00:00 (hours, minutes, seconds)
if (RegEx::match('^([0-9]+-[0-9]+-[0-9]+)$', $date, $match))
{
return $match[1] . ' 00:00:00';
}
// Date format is not correct, so return null
return null;
}
/**
* Applies offset to a date
*
* @param string $date
* @param string $timezone
*/
public static function applyTimezone(&$date, $timezone = '')
{
if ($date <= 0)
{
$date = 0;
return;
}
$timezone = $timezone ?: JFactory::getUser()->getParam('timezone', JFactory::getConfig()->get('offset'));
$date = JFactory::getDate($date, $timezone);
$date->setTimezone(new DateTimeZone('UTC'));
$date = $date->format('Y-m-d H:i:s', true, false);
}
/**
* Convert string with 'date' format to 'strftime' format
*
* @param string $format
*
* @return string
*/
public static function strftimeToDateFormat($format)
{
if (strpos($format, '%') === false)
{
return $format;
}
return strtr((string) $format, self::getStrftimeToDateFormats());
}
/**
* Convert string with 'date' format to 'strftime' format
*
* @param string $format
*
* @return string
*/
public static function dateToStrftimeFormat($format)
{
return strtr((string) $format, self::getDateToStrftimeFormats());
}
private static function getStrftimeToDateFormats()
{
return [
// Day
'%d' => 'd',
'%a' => 'D',
'%#d' => 'j',
'%A' => 'l',
'%u' => 'N',
'%w' => 'w',
'%j' => 'z',
// Week
'%V' => 'W',
// Month
'%B' => 'F',
'%m' => 'm',
'%b' => 'M',
// Year
'%G' => 'o',
'%Y' => 'Y',
'%y' => 'y',
// Time
'%P' => 'a',
'%p' => 'A',
'%l' => 'g',
'%I' => 'h',
'%H' => 'H',
'%M' => 'i',
'%S' => 's',
// Timezone
'%z' => 'O',
'%Z' => 'T',
// Full Date / Time
'%s' => 'U',
];
}
private static function getDateToStrftimeFormats()
{
return [
// Day - no strf eq : S
'd' => '%d',
'D' => '%a',
'jS' => '%#d[TH]',
'j' => '%#d',
'l' => '%A',
'N' => '%u',
'w' => '%w',
'z' => '%j',
// Week - no date eq : %U, %W
'W' => '%V',
// Month - no strf eq : n, t
'F' => '%B',
'm' => '%m',
'M' => '%b',
// Year - no strf eq : L; no date eq : %C, %g
'o' => '%G',
'Y' => '%Y',
'y' => '%y',
// Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X
'a' => '%P',
'A' => '%p',
'g' => '%l',
'h' => '%I',
'H' => '%H',
'i' => '%M',
's' => '%S',
// Timezone - no strf eq : e, I, P, Z
'O' => '%z',
'T' => '%Z',
// Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x
'U' => '%s',
];
}
}