| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeZone; |
| 7 |
|
| 8 |
final class DateTimeHelper |
| 9 |
{ |
| 10 |
private $_dateFormat; |
| 11 |
private $_timeFormat; |
| 12 |
private $_timezone; |
| 13 |
private $_currentTime; |
| 14 |
private $_currentFormat; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
$this->_dateFormat = get_option('date_format'); |
| 19 |
$this->_timeFormat = get_option('time_format'); |
| 20 |
$this->_timezone = self::wp_timezone(); |
| 21 |
$this->_currentTime = current_time('mysql'); |
| 22 |
$this->_currentFormat = 'Y-m-d H:i:s'; |
| 23 |
} |
| 24 |
|
| 25 |
public function getDate($date = null, $currentFormat = null, $currentTZ = null, $expectedFormat = null, $expectedTZ = null) |
| 26 |
{ |
| 27 |
if (is_null($date)) { |
| 28 |
$date = $this->_currentTime; |
| 29 |
$currentFormat = $this->_currentFormat; |
| 30 |
$currentTZ = $this->_timezone; |
| 31 |
} |
| 32 |
|
| 33 |
$currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat; |
| 34 |
$currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ; |
| 35 |
$expectedFormat = is_null($expectedFormat) ? $this->_dateFormat : $expectedFormat; |
| 36 |
$expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ; |
| 37 |
|
| 38 |
return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ); |
| 39 |
} |
| 40 |
|
| 41 |
public function getTime($date = null, $currentFormat = null, $currentTZ = null, $expectedFormat = null, $expectedTZ = null) |
| 42 |
{ |
| 43 |
if (is_null($date)) { |
| 44 |
$date = $this->_currentTime; |
| 45 |
$currentFormat = $this->_currentFormat; |
| 46 |
$currentTZ = $this->_timezone; |
| 47 |
} |
| 48 |
|
| 49 |
$currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat; |
| 50 |
$currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ; |
| 51 |
$expectedFormat = is_null($expectedFormat) ? $this->_timeFormat : $expectedFormat; |
| 52 |
$expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ; |
| 53 |
|
| 54 |
return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ); |
| 55 |
} |
| 56 |
|
| 57 |
public function getDay($nameType, $date = null, $currentFormat = null, $currentTZ = null, $expectedTZ = null) |
| 58 |
{ |
| 59 |
if (is_null($date)) { |
| 60 |
$date = $this->_currentTime; |
| 61 |
$currentFormat = $this->_currentFormat; |
| 62 |
$currentTZ = $this->_timezone; |
| 63 |
} |
| 64 |
|
| 65 |
$currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat; |
| 66 |
$currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ; |
| 67 |
$expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ; |
| 68 |
|
| 69 |
switch ($nameType) { |
| 70 |
case 'numeric-with-leading': |
| 71 |
$expectedFormat = 'd'; |
| 72 |
break; |
| 73 |
|
| 74 |
case 'numeric-without-leading': |
| 75 |
$expectedFormat = 'j'; |
| 76 |
break; |
| 77 |
|
| 78 |
case 'short-name': |
| 79 |
$expectedFormat = 'D'; |
| 80 |
break; |
| 81 |
|
| 82 |
case 'full-name': |
| 83 |
$expectedFormat = 'l'; |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
$expectedFormat = 'd'; |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ); |
| 92 |
} |
| 93 |
|
| 94 |
public function getMonth($nameType, $date = null, $currentFormat = null, $currentTZ = null, $expectedTZ = null) |
| 95 |
{ |
| 96 |
if (is_null($date)) { |
| 97 |
$date = $this->_currentTime; |
| 98 |
$currentFormat = $this->_currentFormat; |
| 99 |
$currentTZ = $this->_timezone; |
| 100 |
} |
| 101 |
|
| 102 |
$currentFormat = is_null($currentFormat) ? $this->_currentFormat : $currentFormat; |
| 103 |
$currentTZ = is_null($currentTZ) ? $this->_timezone : $currentTZ; |
| 104 |
$expectedTZ = is_null($expectedTZ) ? $this->_timezone : $expectedTZ; |
| 105 |
|
| 106 |
switch ($nameType) { |
| 107 |
case 'numeric-with-leading': |
| 108 |
$expectedFormat = 'm'; |
| 109 |
break; |
| 110 |
|
| 111 |
case 'numeric-without-leading': |
| 112 |
$expectedFormat = 'n'; |
| 113 |
break; |
| 114 |
|
| 115 |
case 'short-name': |
| 116 |
$expectedFormat = 'M'; |
| 117 |
break; |
| 118 |
|
| 119 |
case 'full-name': |
| 120 |
$expectedFormat = 'F'; |
| 121 |
break; |
| 122 |
|
| 123 |
default: |
| 124 |
$expectedFormat = 'd'; |
| 125 |
break; |
| 126 |
} |
| 127 |
|
| 128 |
return $this->getFormated($date, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ); |
| 129 |
} |
| 130 |
|
| 131 |
public function getFormated($dateString, $currentFormat, $currentTZ, $expectedFormat, $expectedTZ) |
| 132 |
{ |
| 133 |
if (false === $currentFormat) { |
| 134 |
$dateObject = new DateTime($dateString, $currentTZ); |
| 135 |
} else { |
| 136 |
$dateObject = DateTime::createFromFormat($currentFormat, $dateString, $currentTZ); |
| 137 |
} |
| 138 |
|
| 139 |
if (!is_null($expectedTZ)) { |
| 140 |
$dateObject->setTimezone($expectedTZ); |
| 141 |
} |
| 142 |
|
| 143 |
if ($dateObject) { |
| 144 |
return $dateObject->format($expectedFormat); |
| 145 |
} |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
public function getUnicodeLikeFormat($type, $format = null) |
| 150 |
{ |
| 151 |
$type = strtolower($type); |
| 152 |
switch ($type) { |
| 153 |
case 'date': |
| 154 |
$format = is_null($format) ? $this->_dateFormat : $format; |
| 155 |
break; |
| 156 |
|
| 157 |
case 'time': |
| 158 |
$format = is_null($format) ? $this->_timeFormat : $format; |
| 159 |
break; |
| 160 |
|
| 161 |
case 'timestamp': |
| 162 |
$format = is_null($format) ? $this->_currentFormat : $format; |
| 163 |
break; |
| 164 |
|
| 165 |
default: |
| 166 |
break; |
| 167 |
} |
| 168 |
|
| 169 |
if (false !== strpos($format, 'd')) { |
| 170 |
$format = str_replace('d', 'dd', $format); |
| 171 |
} |
| 172 |
if (false !== strpos($format, 'j')) { |
| 173 |
$format = str_replace('j', 'd', $format); |
| 174 |
} |
| 175 |
if (false !== strpos($format, 'D')) { |
| 176 |
$format = str_replace('D', 'eee', $format); |
| 177 |
} |
| 178 |
if (false !== strpos($format, 'I')) { |
| 179 |
$format = str_replace('I', 'eeee', $format); |
| 180 |
} |
| 181 |
if (false !== strpos($format, 'S')) { |
| 182 |
$format = str_replace('S', 'F', $format); |
| 183 |
} |
| 184 |
if (false !== strpos($format, 'M')) { |
| 185 |
$format = str_replace('M', 'MMM', $format); |
| 186 |
} |
| 187 |
if (false !== strpos($format, 'F')) { |
| 188 |
$format = str_replace('F', 'MMMM', $format); |
| 189 |
} |
| 190 |
if (false !== strpos($format, 'm')) { |
| 191 |
$format = str_replace('m', 'MM', $format); |
| 192 |
} |
| 193 |
if (false !== strpos($format, 'n')) { |
| 194 |
$format = str_replace('n', 'M', $format); |
| 195 |
} |
| 196 |
if (false !== strpos($format, 'y')) { |
| 197 |
$format = str_replace('y', 'yy', $format); |
| 198 |
} |
| 199 |
if (false !== strpos($format, 'Y')) { |
| 200 |
$format = str_replace('Y', 'yyyy', $format); |
| 201 |
} |
| 202 |
if (false !== strpos($format, 'a')) { |
| 203 |
$format = str_replace('a', 'aaaa', $format); |
| 204 |
} |
| 205 |
if (false !== strpos($format, 'A')) { |
| 206 |
$format = str_replace('A', 'aaaa', $format); |
| 207 |
} |
| 208 |
if (false !== strpos($format, 'g')) { |
| 209 |
$format = str_replace('g', 'h', $format); |
| 210 |
} |
| 211 |
if (false !== strpos($format, 'G')) { |
| 212 |
$format = str_replace('G', 'H', $format); |
| 213 |
} |
| 214 |
if (false !== strpos($format, 'h')) { |
| 215 |
$format = str_replace('h', 'hh', $format); |
| 216 |
} |
| 217 |
if (false !== strpos($format, 'H')) { |
| 218 |
$format = str_replace('H', 'HH', $format); |
| 219 |
} |
| 220 |
if (false !== strpos($format, 'i')) { |
| 221 |
$format = str_replace('i', 'mm', $format); |
| 222 |
} |
| 223 |
if (false !== strpos($format, 's')) { |
| 224 |
$format = str_replace('s', 'ss', $format); |
| 225 |
} |
| 226 |
return $format; |
| 227 |
} |
| 228 |
|
| 229 |
public function getUnicodeToPhpFormat($type, $format = null) |
| 230 |
{ |
| 231 |
$type = strtolower($type); |
| 232 |
switch ($type) { |
| 233 |
case 'date': |
| 234 |
$format = is_null($format) ? $this->_dateFormat : $format; |
| 235 |
break; |
| 236 |
|
| 237 |
case 'time': |
| 238 |
$format = is_null($format) ? $this->_timeFormat : $format; |
| 239 |
break; |
| 240 |
|
| 241 |
case 'timestamp': |
| 242 |
$format = is_null($format) ? $this->_currentFormat : $format; |
| 243 |
break; |
| 244 |
default: |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
if (false !== strpos($format, 'd')) { |
| 249 |
$format = str_replace('dd', 'd', $format); |
| 250 |
} |
| 251 |
if (false !== strpos($format, 'E')) { |
| 252 |
$format = str_replace('E', 'D', $format); |
| 253 |
} |
| 254 |
if (false !== strpos($format, 'MMMM')) { |
| 255 |
$format = str_replace('MMMM', 'F', $format); |
| 256 |
} elseif (false !== strpos($format, 'MMM')) { |
| 257 |
$format = str_replace('MMM', 'M', $format); |
| 258 |
} elseif (false !== strpos($format, 'MM')) { |
| 259 |
$format = str_replace('MM', 'm', $format); |
| 260 |
} |
| 261 |
if (false !== strpos($format, 'yyyy')) { |
| 262 |
$format = str_replace('yyyy', 'Y', $format); |
| 263 |
} elseif (false !== strpos($format, 'yy')) { |
| 264 |
$format = str_replace('yy', 'y', $format); |
| 265 |
} |
| 266 |
return $format; |
| 267 |
} |
| 268 |
|
| 269 |
public static function wp_timezone_string() |
| 270 |
{ |
| 271 |
if (\function_exists('wp_timezone_string')) { |
| 272 |
return wp_timezone_string(); |
| 273 |
} |
| 274 |
|
| 275 |
$timezone_string = get_option('timezone_string'); |
| 276 |
|
| 277 |
if ($timezone_string) { |
| 278 |
return $timezone_string; |
| 279 |
} |
| 280 |
|
| 281 |
$offset = (float) get_option('gmt_offset'); |
| 282 |
$hours = (int) $offset; |
| 283 |
$minutes = ($offset - $hours); |
| 284 |
|
| 285 |
$sign = ($offset < 0) ? '-' : '+'; |
| 286 |
$abs_hour = abs($hours); |
| 287 |
$abs_mins = abs($minutes * 60); |
| 288 |
$tz_offset = sprintf('%s%02d:%02d', $sign, $abs_hour, $abs_mins); |
| 289 |
|
| 290 |
return $tz_offset; |
| 291 |
} |
| 292 |
|
| 293 |
public static function wp_timezone() |
| 294 |
{ |
| 295 |
if (\function_exists('wp_timezone')) { |
| 296 |
return wp_timezone(); |
| 297 |
} |
| 298 |
return new DateTimeZone(self::wp_timezone_string()); |
| 299 |
} |
| 300 |
} |
| 301 |
|