| 1 |
<?php |
| 2 |
|
| 3 |
function acym_replaceDateTags(string $value): string |
| 4 |
{ |
| 5 |
$replace = ['{year}', '{month}', '{weekday}', '{day}']; |
| 6 |
$replaceBy = [date('Y'), date('m'), date('N'), date('d')]; |
| 7 |
$value = str_replace($replace, $replaceBy, $value); |
| 8 |
|
| 9 |
$results = []; |
| 10 |
if (preg_match_all('#{(year|month|weekday|day)\|(add|remove):([^}]*)}#Uis', $value, $results)) { |
| 11 |
foreach ($results[0] as $i => $oneMatch) { |
| 12 |
$format = str_replace(['year', 'month', 'weekday', 'day'], ['Y', 'm', 'N', 'd'], $results[1][$i]); |
| 13 |
$delay = str_replace(['add', 'remove'], ['+', '-'], $results[2][$i]).intval($results[3][$i]).' '.str_replace('weekday', 'day', $results[1][$i]); |
| 14 |
$value = str_replace($oneMatch, date($format, strtotime($delay)), $value); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
return $value; |
| 19 |
} |
| 20 |
|
| 21 |
function acym_dateField(string $name, $value = '', string $class = '', string $attributes = '', string $relativeDefault = '-'): string |
| 22 |
{ |
| 23 |
$result = '<div class="grid-x margin-y date_rs_selection_popup">'; |
| 24 |
|
| 25 |
$result .= '<div class="cell grid-x">'; |
| 26 |
$result .= acym_switchFilter( |
| 27 |
[ |
| 28 |
'relative' => acym_translation('ACYM_RELATIVE_DATE'), |
| 29 |
'specific' => acym_translation('ACYM_SPECIFIC_DATE'), |
| 30 |
], |
| 31 |
'relative', |
| 32 |
'switch_'.$name, |
| 33 |
'date_rs_selection' |
| 34 |
); |
| 35 |
$result .= '</div>'; |
| 36 |
|
| 37 |
$result .= '<div class="cell date_rs_selection_choice date_rs_selection_relative grid-x grid-margin-x align-center"> |
| 38 |
<div class="cell medium-4"> |
| 39 |
<input type="number" class="relativenumber" value="0"> |
| 40 |
</div> |
| 41 |
<div class="cell medium-4">'; |
| 42 |
$result .= acym_select( |
| 43 |
[ |
| 44 |
'60' => acym_translation('ACYM_MINUTES'), |
| 45 |
'3600' => acym_translation('ACYM_HOUR'), |
| 46 |
'86400' => acym_translation('ACYM_DAY'), |
| 47 |
], |
| 48 |
'relative_'.$name, |
| 49 |
null, |
| 50 |
['class' => 'acym__select relativetype'] |
| 51 |
); |
| 52 |
|
| 53 |
$result .= '</div> |
| 54 |
<div class="cell medium-4">'; |
| 55 |
|
| 56 |
$result .= acym_select( |
| 57 |
[ |
| 58 |
'-' => acym_translation('ACYM_IN_PAST'), |
| 59 |
'+' => acym_translation('ACYM_IN_FUTURE'), |
| 60 |
], |
| 61 |
'relativewhen_'.$name, |
| 62 |
$relativeDefault, |
| 63 |
['class' => 'acym__select relativewhen'] |
| 64 |
); |
| 65 |
$result .= '</div> |
| 66 |
</div>'; |
| 67 |
|
| 68 |
$result .= '<div class="cell date_rs_selection_choice date_rs_selection_specific grid-x align-center acym_vcenter" style="display: none;"> |
| 69 |
<span class="cell shrink margin-right-1">'.acym_translation('ACYM_CHOOSE_DATE').'</span> |
| 70 |
<div class="cell shrink"> |
| 71 |
<input type="text" name="specific_'.acym_escape($name).'" class="acy_date_picker" data-acym-translate="0" readonly> |
| 72 |
</div> |
| 73 |
</div> |
| 74 |
<div class="cell grid-x grid-margin-x"> |
| 75 |
<div class="cell auto"></div> |
| 76 |
<button type="button" class="cell medium-4 button button-secondary acym__button__clear__time" data-close>'.acym_translation('ACYM_CLEAR').'</button> |
| 77 |
<button type="button" class="cell medium-4 button acym__button__set__time" data-close>'.acym_translation('ACYM_APPLY').'</button> |
| 78 |
<div class="cell auto"></div> |
| 79 |
</div>'; |
| 80 |
|
| 81 |
$result .= '</div>'; |
| 82 |
|
| 83 |
$id = 'acym_'.preg_replace('#[^a-z0-9_]#i', '', $name); |
| 84 |
if (is_numeric($value)) { |
| 85 |
$months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; |
| 86 |
|
| 87 |
$replaceValues = []; |
| 88 |
foreach ($months as $oneMonth) { |
| 89 |
$replaceValues[] = substr(acym_translation('ACYM_'.strtoupper($oneMonth)), 0, 3); |
| 90 |
} |
| 91 |
|
| 92 |
$shownValue = str_replace($months, $replaceValues, acym_date($value, 'd F Y H:i')); |
| 93 |
} else { |
| 94 |
$shownValue = $value; |
| 95 |
} |
| 96 |
$result = '<input data-rs="'.acym_escape($id).'" type="hidden" name="'.acym_escape($name).'" value="'.acym_escape($value).'">'.acym_modal( |
| 97 |
'<input data-open="'.acym_escape($id).'" class="rs_date_field '.$class.'" '.$attributes.' type="text" value="'.acym_escape($shownValue).'" readonly>', |
| 98 |
$result, |
| 99 |
$id, |
| 100 |
[], |
| 101 |
[], |
| 102 |
false, |
| 103 |
false |
| 104 |
); |
| 105 |
|
| 106 |
return $result; |
| 107 |
} |
| 108 |
|
| 109 |
function acym_getDate($time = 0, string $format = '%d %B %Y %H:%M') |
| 110 |
{ |
| 111 |
if (empty($time)) return ''; |
| 112 |
|
| 113 |
if (is_numeric($format)) { |
| 114 |
$format = acym_translation('ACYM_DATE_FORMAT_LC'.$format); |
| 115 |
} |
| 116 |
|
| 117 |
$format = str_replace( |
| 118 |
['%A', '%d', '%B', '%m', '%Y', '%y', '%H', '%M', '%S', '%a', '%I', '%p', '%w'], |
| 119 |
['l', 'd', 'F', 'm', 'Y', 'y', 'H', 'i', 's', 'D', 'h', 'a', 'w'], |
| 120 |
$format |
| 121 |
); |
| 122 |
|
| 123 |
try { |
| 124 |
return acym_date($time, $format, false); |
| 125 |
} catch (Exception $e) { |
| 126 |
return date($format, $time); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
function acym_replaceDate(string $mydate, bool $display = false): string |
| 131 |
{ |
| 132 |
if (strpos($mydate, '[time]') === false) { |
| 133 |
if (is_numeric($mydate) && $display) { |
| 134 |
return acym_date($mydate, 'Y-m-d H:i:s'); |
| 135 |
} |
| 136 |
|
| 137 |
return $mydate; |
| 138 |
} |
| 139 |
|
| 140 |
if ($mydate === '[time]' && $display) { |
| 141 |
return acym_translation('ACYM_NOW'); |
| 142 |
} |
| 143 |
|
| 144 |
$mydate = str_replace('[time]', time(), $mydate); |
| 145 |
$operators = ['+', '-']; |
| 146 |
foreach ($operators as $oneOperator) { |
| 147 |
if (strpos($mydate, $oneOperator) === false) continue; |
| 148 |
|
| 149 |
$dateArray = explode($oneOperator, $mydate); |
| 150 |
if ($oneOperator == '+') { |
| 151 |
if ($display) { |
| 152 |
$mydate = acym_translationSprintf('ACYM_AFTER_DATE', acym_secondsToTime(intval($dateArray[1]))); |
| 153 |
} else { |
| 154 |
$mydate = intval($dateArray[0]) + intval($dateArray[1]); |
| 155 |
} |
| 156 |
} elseif ($oneOperator == '-') { |
| 157 |
if ($display) { |
| 158 |
$mydate = acym_translationSprintf('ACYM_BEFORE_DATE', acym_secondsToTime(intval($dateArray[1]))); |
| 159 |
} else { |
| 160 |
$mydate = intval($dateArray[0]) - intval($dateArray[1]); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return $mydate; |
| 166 |
} |
| 167 |
|
| 168 |
function acym_secondsToTime(int $seconds): string |
| 169 |
{ |
| 170 |
$dtF = new \DateTime('@0'); |
| 171 |
$dtT = new \DateTime("@$seconds"); |
| 172 |
|
| 173 |
return $dtF->diff($dtT)->format('%a day(s) %h h, %i min'); |
| 174 |
} |
| 175 |
|
| 176 |
function acym_displayDateFormat(string $format, string $name = 'date', string $default = '', array $attributes = [], bool $returnHtml = true): string |
| 177 |
{ |
| 178 |
if (empty($attributes)) { |
| 179 |
$attributes = [ |
| 180 |
'class' => 'acym__custom__fields__select__form acym__select', |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
$return = []; |
| 185 |
if ($returnHtml) { |
| 186 |
$return[] = '<div class="cell grid-x grid-margin-x">'; |
| 187 |
} |
| 188 |
|
| 189 |
$days = [ |
| 190 |
'' => acym_translation('ACYM_DAY'), |
| 191 |
]; |
| 192 |
for ($i = 1; $i <= 31; $i++) { |
| 193 |
$days[$i < 10 ? '0'.$i : $i] = $i < 10 ? '0'.$i : $i; |
| 194 |
} |
| 195 |
|
| 196 |
$month = [ |
| 197 |
'' => acym_translation('ACYM_MONTH'), |
| 198 |
'01' => acym_translation('ACYM_JANUARY'), |
| 199 |
'02' => acym_translation('ACYM_FEBRUARY'), |
| 200 |
'03' => acym_translation('ACYM_MARCH'), |
| 201 |
'04' => acym_translation('ACYM_APRIL'), |
| 202 |
'05' => acym_translation('ACYM_MAY'), |
| 203 |
'06' => acym_translation('ACYM_JUNE'), |
| 204 |
'07' => acym_translation('ACYM_JULY'), |
| 205 |
'08' => acym_translation('ACYM_AUGUST'), |
| 206 |
'09' => acym_translation('ACYM_SEPTEMBER'), |
| 207 |
'10' => acym_translation('ACYM_OCTOBER'), |
| 208 |
'11' => acym_translation('ACYM_NOVEMBER'), |
| 209 |
'12' => acym_translation('ACYM_DECEMBER'), |
| 210 |
]; |
| 211 |
|
| 212 |
$year = [ |
| 213 |
'' => acym_translation('ACYM_YEAR'), |
| 214 |
]; |
| 215 |
$currentYear = (int)acym_date('now', 'Y'); |
| 216 |
for ($i = 1900; $i <= $currentYear + 10; $i++) { |
| 217 |
$year[$i] = $i; |
| 218 |
} |
| 219 |
|
| 220 |
$formatToDisplay = explode('%', $format); |
| 221 |
$defaultDate = empty($default) ? [] : explode('-', $default); |
| 222 |
|
| 223 |
unset($formatToDisplay[0]); |
| 224 |
foreach ($formatToDisplay as $one) { |
| 225 |
if ($one === 'd') { |
| 226 |
if ($returnHtml) { |
| 227 |
$return[] = '<div class="medium-3 margin-left-0 margin-right-0 cell">'.acym_select( |
| 228 |
$days, |
| 229 |
$name, |
| 230 |
empty($defaultDate[2]) || $defaultDate[2] === '00' ? '' : $defaultDate[2], |
| 231 |
$attributes, |
| 232 |
'value', |
| 233 |
'text', |
| 234 |
$name.'-'.$one |
| 235 |
).'</div>'; |
| 236 |
} elseif (!empty($defaultDate[2]) && $defaultDate[2] !== '00') { |
| 237 |
$return[] = $defaultDate[2]; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if ($one === 'm') { |
| 242 |
if ($returnHtml) { |
| 243 |
$return[] = '<div class="medium-5 cell">'.acym_select( |
| 244 |
$month, |
| 245 |
$name, |
| 246 |
empty($defaultDate[1]) || $defaultDate[1] === '00' ? '' : $defaultDate[1], |
| 247 |
$attributes, |
| 248 |
'value', |
| 249 |
'text', |
| 250 |
$name.'-'.$one |
| 251 |
).'</div>'; |
| 252 |
} elseif (!empty($defaultDate[1]) && $defaultDate[1] !== '00') { |
| 253 |
$return[] = $month[$defaultDate[1]]; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if ($one === 'y') { |
| 258 |
if ($returnHtml) { |
| 259 |
$return[] = '<div class="medium-4 margin-right-0 cell">'.acym_select( |
| 260 |
$year, |
| 261 |
$name, |
| 262 |
empty($defaultDate[0]) || $defaultDate[0] === '0000' ? '' : $defaultDate[0], |
| 263 |
$attributes, |
| 264 |
'value', |
| 265 |
'text', |
| 266 |
$name.'-'.$one |
| 267 |
).'</div>'; |
| 268 |
} elseif (!empty($defaultDate[0]) && $defaultDate[0] !== '0000') { |
| 269 |
$return[] = $defaultDate[0]; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if ($returnHtml) { |
| 275 |
$return[] = '</div>'; |
| 276 |
|
| 277 |
return implode('', $return); |
| 278 |
} else { |
| 279 |
return implode(' ', $return); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
function acym_getTimeFromUTCDate(?string $date): int |
| 284 |
{ |
| 285 |
if (empty($date)) { |
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
$timestamp = strtotime($date); |
| 290 |
if ($timestamp === false) { |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
return $timestamp + date('Z'); |
| 295 |
} |
| 296 |
|
| 297 |
function acym_getTimeFromCMSDate($date) |
| 298 |
{ |
| 299 |
return acym_getTimeFromUTCDate($date) - acym_getTimeOffsetCMS(); |
| 300 |
} |
| 301 |
|
| 302 |
function acym_getTime($date) |
| 303 |
{ |
| 304 |
return acym_getTimeFromCMSDate($date); |
| 305 |
} |
| 306 |
|
| 307 |
function acym_date($time = 'now', $format = null, bool $useTz = true, bool $translate = true): string |
| 308 |
{ |
| 309 |
if ($time === 'now') { |
| 310 |
$time = time(); |
| 311 |
} |
| 312 |
|
| 313 |
if (is_numeric($time)) { |
| 314 |
$time = acym_dateTimeCMS($time); |
| 315 |
} |
| 316 |
|
| 317 |
if (!$format || (strpos($format, 'ACYM_DATE_FORMAT') !== false && acym_translation($format) == $format)) { |
| 318 |
$format = 'ACYM_DATE_FORMAT_LC1'; |
| 319 |
} |
| 320 |
if (strpos($format, 'ACYM_DATE') !== false) { |
| 321 |
$format = acym_translation($format); |
| 322 |
} |
| 323 |
|
| 324 |
if ($useTz === false) { |
| 325 |
$date = new DateTime($time); |
| 326 |
|
| 327 |
if ($translate) { |
| 328 |
return acym_translateDate($date->format($format)); |
| 329 |
} else { |
| 330 |
return $date->format($format); |
| 331 |
} |
| 332 |
} else { |
| 333 |
$cmsOffset = str_replace('.', ':', acym_getCMSConfig('offset')); |
| 334 |
|
| 335 |
$timezone = new DateTimeZone($cmsOffset); |
| 336 |
|
| 337 |
if (!is_numeric($cmsOffset)) { |
| 338 |
$cmsOffset = $timezone->getOffset(new DateTime()); |
| 339 |
} |
| 340 |
|
| 341 |
if ($translate) { |
| 342 |
return acym_translateDate(date($format, strtotime($time) + $cmsOffset)); |
| 343 |
} else { |
| 344 |
return date($format, strtotime($time) + $cmsOffset); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
function acym_translateDate(string $date): string |
| 350 |
{ |
| 351 |
$map = [ |
| 352 |
'January' => 'ACYM_JANUARY', |
| 353 |
'February' => 'ACYM_FEBRUARY', |
| 354 |
'March' => 'ACYM_MARCH', |
| 355 |
'April' => 'ACYM_APRIL', |
| 356 |
'May' => 'ACYM_MAY', |
| 357 |
'June' => 'ACYM_JUNE', |
| 358 |
'July' => 'ACYM_JULY', |
| 359 |
'August' => 'ACYM_AUGUST', |
| 360 |
'September' => 'ACYM_SEPTEMBER', |
| 361 |
'October' => 'ACYM_OCTOBER', |
| 362 |
'November' => 'ACYM_NOVEMBER', |
| 363 |
'December' => 'ACYM_DECEMBER', |
| 364 |
'Monday' => 'ACYM_MONDAY', |
| 365 |
'Tuesday' => 'ACYM_TUESDAY', |
| 366 |
'Wednesday' => 'ACYM_WEDNESDAY', |
| 367 |
'Thursday' => 'ACYM_THURSDAY', |
| 368 |
'Friday' => 'ACYM_FRIDAY', |
| 369 |
'Saturday' => 'ACYM_SATURDAY', |
| 370 |
'Sunday' => 'ACYM_SUNDAY', |
| 371 |
]; |
| 372 |
|
| 373 |
foreach ($map as $english => $translationKey) { |
| 374 |
$translation = acym_translation($translationKey); |
| 375 |
if ($translation === $translationKey) { |
| 376 |
continue; |
| 377 |
} |
| 378 |
|
| 379 |
$date = preg_replace('#'.preg_quote($english).'#i', $translation, $date); |
| 380 |
$date = preg_replace('#'.preg_quote(substr($english, 0, 3)).'#i', mb_substr($translation, 0, 3), $date); |
| 381 |
} |
| 382 |
|
| 383 |
return $date; |
| 384 |
} |
| 385 |
|
| 386 |
function acym_isDateValid($date): bool |
| 387 |
{ |
| 388 |
return $date !== '0000-00-00 00:00:00' && !empty($date); |
| 389 |
} |
| 390 |
|