3declare(strict_types=1);
23 public const SYSTEM_DEFAULT_STR =
'System Default';
32 public static $instance;
42 public string $format =
"%d.%m.%Y";
44 public string $sINTLFormat =
"d.M.yyyy";
54 public array $lang = [
'de_DE@euro',
'de_DE.UTF-8',
'de_DE',
'de',
'ge'];
63 public string $mode =
"LC_ALL";
75 public int $force_year = 2;
84 public array $CORE_date_formats_PHP = [
85 'l, jS F, Y'=>
'%A, %e %B, %Y',
86 'jS F, Y' =>
'%e %B, %Y',
87 'd M Y' =>
'%d %a %Y',
88 'M d Y' =>
'%a %d %Y',
89 'D M d, Y' =>
'%a %b %d, %Y', ##
90 'd-m-Y' =>
'%d-%m-%Y', #1
91 'm-d-Y' =>
'%m-%d-%Y',
92 'd.m.Y' =>
'%d.%m.%Y', #2
93 'm.d.Y' =>
'%m.%d.%Y',
94 'd/m/Y' =>
'%d/%m/%Y', #3
95 'm/d/Y' =>
'%m/%d/%Y',
96 'j.n.Y' =>
'%e.%n.%Y' #4! Day in month without leading zero
106 public array $CORE_date_formats_MYSQL = [
107 'l, jS F, Y'=>
'%W, %D %M, %Y',
108 'jS F, Y' =>
'%D %M, %Y',
109 'd M Y' =>
'%e. %M %Y',
110 'M d Y' =>
'%b %e %Y',
111 'D M d, Y' =>
'%a %b %e, %Y',
112 'd-m-Y' =>
'%d-%m-%Y',
113 'm-d-Y' =>
'%m-%d-%Y',
114 'd.m.Y' =>
'%d.%m.%Y',
115 'm.d.Y' =>
'%m.%d.%Y',
116 'd/m/Y' =>
'%d/%m/%Y',
117 'm/d/Y' =>
'%m/%d/%Y',
118 'j.n.Y' =>
'%e.%c.%Y',
119 'Y-m-d' =>
'%Y-%m-%d',
120 'Y.m.d' =>
'%Y.%m.%d'
130 public array $CORE_time_formats_PHP = [
131 'g:i A' =>
'%I:%M %p',
132 'g:i a' =>
'%I:%M %P',
133 'H:i:s' =>
'%H:%M:%S',
144 public array $CORE_time_formats_MYSQL = [
145 'g:i A' =>
'%l:%i %p',
147 'H:i:s' =>
'%H:%i:%s',
158 public array $CORE_date_formats_DatePicker = [
159 'l, jS F, Y'=>
'DD, d. MM yy',
160 'jS F, Y' =>
'd. MM, yy',
161 'd M Y' =>
'd. MM yy',
163 'D M d, Y' =>
'D M d, yy',
168 'Y-m-d' =>
'yy-mm-dd',
169 'Y.m.d' =>
'yy.mm.dd',
185 public bool $useINTL =
false;
186 public bool $intl_installed =
false;
195 public array $CORE_date_formats_INTL = [
196 'l, jS F, Y'=>
'A, e B, yyyy',
197 'jS F, Y' =>
'e B, yyyy',
198 'd M Y' =>
'd M yyyy',
199 'M d Y' =>
'M d yyyy',
200 'D M d, Y' =>
'EE d MM, yyyy',
207 'j.n.Y' =>
'd.M.yyyy'
214 public const MYSQL_LOCALES = [
215 "ar_AE" =>
"Arabic - United Arab Emirates",
216 "ar_BH" =>
"Arabic - Bahrain",
217 "ar_DZ" =>
"Arabic - Algeria",
218 "ar_EG" =>
"Arabic - Egypt",
219 "ar_IN" =>
"Arabic - India",
220 "ar_IQ" =>
"Arabic - Iraq",
221 "ar_JO" =>
"Arabic - Jordan",
222 "ar_KW" =>
"Arabic - Kuwait",
223 "ar_LB" =>
"Arabic - Lebanon",
224 "ar_LY" =>
"Arabic - Libya",
225 "ar_MA" =>
"Arabic - Morocco",
226 "ar_OM" =>
"Arabic - Oman",
227 "ar_QA" =>
"Arabic - Qatar",
228 "ar_SA" =>
"Arabic - Saudi Arabia",
229 "ar_SD" =>
"Arabic - Sudan",
230 "ar_SY" =>
"Arabic - Syria",
231 "ar_TN" =>
"Arabic - Tunisia",
232 "ar_YE" =>
"Arabic - Yemen",
233 "be_BY" =>
"Belarusian - Belarus",
234 "bg_BG" =>
"Bulgarian - Bulgaria",
235 "ca_ES" =>
"Catalan - Spain",
236 "cs_CZ" =>
"Czech - Czech Republic",
237 "da_DK" =>
"Danish - Denmark",
238 "de_AT" =>
"German - Austria",
239 "de_BE" =>
"German - Belgium",
240 "de_CH" =>
"German - Switzerland",
241 "de_DE" =>
"German - Germany",
242 "de_LU" =>
"German - Luxembourg",
243 "el_GR" =>
"Greek - Greece",
244 "en_AU" =>
"English - Australia",
245 "en_CA" =>
"English - Canada",
246 "en_GB" =>
"English - United Kingdom",
247 "en_IN" =>
"English - India",
248 "en_NZ" =>
"English - New Zealand",
249 "en_PH" =>
"English - Philippines",
250 "en_US" =>
"English - United States",
251 "en_ZA" =>
"English - South Africa",
252 "en_ZW" =>
"English - Zimbabwe",
253 "es_AR" =>
"Spanish - Argentina",
254 "es_BO" =>
"Spanish - Bolivia",
255 "es_CL" =>
"Spanish - Chile",
256 "es_CO" =>
"Spanish - Colombia",
257 "es_CR" =>
"Spanish - Costa Rica",
258 "es_DO" =>
"Spanish - Dominican Republic",
259 "es_EC" =>
"Spanish - Ecuador",
260 "es_ES" =>
"Spanish - Spain",
261 "es_GT" =>
"Spanish - Guatemala",
262 "es_HN" =>
"Spanish - Honduras",
263 "es_MX" =>
"Spanish - Mexico",
264 "es_NI" =>
"Spanish - Nicaragua",
265 "es_PA" =>
"Spanish - Panama",
266 "es_PE" =>
"Spanish - Peru",
267 "es_PR" =>
"Spanish - Puerto Rico",
268 "es_PY" =>
"Spanish - Paraguay",
269 "es_SV" =>
"Spanish - El Salvador",
270 "es_US" =>
"Spanish - United States",
271 "es_UY" =>
"Spanish - Uruguay",
272 "es_VE" =>
"Spanish - Venezuela",
273 "et_EE" =>
"Estonian - Estonia",
274 "eu_ES" =>
"Basque - Spain",
275 "fi_FI" =>
"Finnish - Finland",
276 "fo_FO" =>
"Faroese - Faroe Islands",
277 "fr_BE" =>
"French - Belgium",
278 "fr_CA" =>
"French - Canada",
279 "fr_CH" =>
"French - Switzerland",
280 "fr_FR" =>
"French - France",
281 "fr_LU" =>
"French - Luxembourg",
282 "gl_ES" =>
"Galician - Spain",
283 "gu_IN" =>
"Gujarati - India",
284 "he_IL" =>
"Hebrew - Israel",
285 "hi_IN" =>
"Hindi - India",
286 "hr_HR" =>
"Croatian - Croatia",
287 "hu_HU" =>
"Hungarian - Hungary",
288 "id_ID" =>
"Indonesian - Indonesia",
289 "is_IS" =>
"Icelandic - Iceland",
290 "it_CH" =>
"Italian - Switzerland",
291 "it_IT" =>
"Italian - Italy",
292 "ja_JP" =>
"Japanese - Japan",
293 "ko_KR" =>
"Korean - Republic of Korea",
294 "lt_LT" =>
"Lithuanian - Lithuania",
295 "lv_LV" =>
"Latvian - Latvia",
296 "mk_MK" =>
"Macedonian - North Macedonia",
297 "mn_MN" =>
"Mongolia - Mongolian",
298 "ms_MY" =>
"Malay - Malaysia",
299 "nb_NO" =>
"Norwegian(Bokmål) - Norway",
300 "nl_BE" =>
"Dutch - Belgium",
301 "nl_NL" =>
"Dutch - The Netherlands",
302 "no_NO" =>
"Norwegian - Norway",
303 "pl_PL" =>
"Polish - Poland",
304 "pt_BR" =>
"Portugese - Brazil",
305 "pt_PT" =>
"Portugese - Portugal",
306 "rm_CH" =>
"Romansh - Switzerland",
307 "ro_RO" =>
"Romanian - Romania",
308 "ru_RU" =>
"Russian - Russia",
309 "ru_UA" =>
"Russian - Ukraine",
310 "sk_SK" =>
"Slovak - Slovakia",
311 "sl_SI" =>
"Slovenian - Slovenia",
312 "sq_AL" =>
"Albanian - Albania",
313 "sr_RS" =>
"Serbian - Serbia",
314 "sv_FI" =>
"Swedish - Finland",
315 "sv_SE" =>
"Swedish - Sweden",
316 "ta_IN" =>
"Tamil - India",
317 "te_IN" =>
"Telugu - India",
318 "th_TH" =>
"Thai - Thailand",
319 "tr_TR" =>
"Turkish - Turkey",
320 "uk_UA" =>
"Ukrainian - Ukraine",
321 "ur_PK" =>
"Urdu - Pakistan",
322 "vi_VN" =>
"Vietnamese - Vietnam",
323 "zh_CN" =>
"Chinese - China",
324 "zh_HK" =>
"Chinese - Hong Kong",
325 "zh_TW" =>
"Chinese - Taiwan"
335 public static function getInstance(
bool $bUseINTL =
false): object
337 if (
null === static::$instance) {
338 static::$instance =
new static();
340 static::$instance->useINTL = $bUseINTL;
342 if (
true === $bUseINTL)
344 static::$instance->intl_installed = static::$instance->checkINTLExtensionExists();
347 return static::$instance;
353 protected function __construct ()
364 public function addLanguage ($aString=
""): bool
366 if ($aString ==
"") {
369 if (
false === in_array($aString, $this->lang)) {
370 $this->lang[] = $aString;
381 public function setFormat(
string $aString=
"" ): bool
385 $this->sINTLFormat = $aString;
387 $this->format=$aString;
403 public function toHTML(
int $iTimestamp = 0 ): string
405 if (NULL == $iTimestamp)
407 $iTimestamp = TIME();
410 $aTempLocale = setlocale(LC_ALL, $this->lang);
411 if (
false === $aTempLocale)
413 $aTempLocale = setlocale(LC_ALL, 0);
416 if ((
true === $this->useINTL) && (
true === $this->intl_installed))
418 $fmt = datefmt_create(
420 IntlDateFormatter::FULL,
421 IntlDateFormatter::FULL,
422 DEFAULT_TIMEZONE_STRING,
423 IntlDateFormatter::GREGORIAN,
430 $this->useINTL =
false;
431 return date($this->format, $iTimestamp);
435 $sFormatedDate = datefmt_format($fmt, $iTimestamp );
437 return $sFormatedDate;
445 if (class_exists(
"IntlDateFormatter"))
447 $oTempFormatter =
new IntlDateFormatter(
449 IntlDateFormatter::LONG,
450 IntlDateFormatter::NONE,
451 DEFAULT_TIMEZONE_STRING,
452 IntlDateFormatter::GREGORIAN,
456 return $oTempFormatter->format($iTimestamp);
463 return date(str_replace(
"%",
"",$this->format), $iTimestamp);
478 public function setLanguage(array $aArray = []): bool
481 foreach ($aArray as $a)
505 public function transform (
string $aDateString =
"01.01.1971",
string $aFormat=
"dmy"): string
507 $this->_force_date ($aDateString);
508 $this->_force_format ($aFormat);
510 $temp = explode(
".", $aDateString);
520 $this->_force_year($temp[2]);
521 $temp_time = mktime( 1, 0, 0, $temp[1], $temp[0], $temp[2]);
525 $this->_force_year($temp[2]);
526 $temp_time = mktime( 1, 0, 0, $temp[0], $temp[1], $temp[2]);
530 $this->_force_year($temp[0]);
531 $temp_time = mktime( 1, 0, 0, $temp[1], $temp[2], $temp[0]);
545 return $this->toHTML($temp_time);
563 private function _force_date(
string &$aDateString ): void
565 $pattern = [
"*[\\/|.|-]+*"];
568 $aDateString = preg_replace($pattern, $replace, $aDateString);
582 private function _force_format(
string &$aFormat): void
585 $aFormat = strtolower ($aFormat);
587 $pattern = [
"*[\\/|.|-]+*",
"*[ |%]+*"];
590 $aFormat = preg_replace($pattern, $replace, $aFormat);
602 private function _force_year(
string|
int &$aYearStr =
"1971"): void
604 $aYearStr = (string) $aYearStr;
606 if (strlen($aYearStr) == 2)
608 $aYearStr = (((int) $aYearStr > $this->force_year + (int) DATE(
"y", TIME())) ?
"19" :
"20").$aYearStr;
610 if (strlen($aYearStr) > 4)
612 $aYearStr = substr($aYearStr, 0, 4);
615 $aYearStr = intval($aYearStr);
625 public function parse_string (
string &$aStr =
"",
string $aPattern =
"/([0-3][0-9].[01]{0,1}[0-9].[0-9]{2,4})/s"): void
628 preg_match_all($aPattern, $aStr, $found );
629 foreach ($found[1] as $a)
631 $aStr = str_replace($a, $this->transform($a), $aStr);
643 public function set_core_language(
string $aKeyStr =
""): bool
646 $return_value =
true;
652 $this->lang = [
'de_DE.UTF-8',
'de_DE@euro',
'de_DE',
'de',
'ge'];
656 $this->lang = [
'en_EN@euro',
'en_EN',
'en',
'EN',
'en_UK',
'UK',
'en_US',
'en_GB',
'en_CA'];
660 $this->lang = [
'FR',
'fr_FR.UTF-8',
'fr_FR',
'fr_FR@euro',
'fr'];
664 $this->lang = [
'it_IT@euro',
'it_IT',
'it'];
668 $this->lang = [
'nl_NL@euro',
'nl_NL',
'nl',
'Dutch',
'nld_nld'];
672 $this->lang = [
'RU',
'ru_RU.UTF-8',
'ru_RU',
'ru_RU@euro',
'ru'];
676 $this->lang = [
'zh_CN',
'zh_CN.eucCN',
'zh_CN.GB18030',
'zh_CN.GB2312',
'zh_CN.GBK',
'zh_CN.UTF-8',
'zh_HK',
'zh_HK.Big5HKSCS',
'zh_HK.UTF-8',
'zh_TW',
'zh_TW.Big5',
'zh_TW.UTF-8'];
680 $this->test_locale($aKeyStr);
683 return $return_value;
700 public function test_locale (
string $aKey =
"de_DE",
bool $use_it =
true): array
702 if (strlen($aKey) == 2)
704 $aKey = strtolower($aKey).
"_".strtoupper($aKey);
709 exec(
'locale -a', $temp_array);
713 foreach($temp_array as $lang_key)
715 if (substr($lang_key, 0,5) == $aKey)
721 if (!empty($all) && (
true === $use_it))
735 public function formatToDatepicker(
string $sFormatString =
""): string
737 if (isset($this->CORE_date_formats_DatePicker[$sFormatString])) {
738 return $this->CORE_date_formats_DatePicker[$sFormatString];
739 } elseif (isset($this->CORE_date_formats_DatePicker[DATE_FORMAT])) {
740 return $this->CORE_date_formats_DatePicker[DATE_FORMAT];
754 public function calendarToTimestamp(
string $str =
"",
int $offset = 0): int
757 if ($str ==
'0' || $str ==
'')
764 if(preg_match(
'/^\d{1,2}\.\d{1,2}\.\d{2}(\d{2})?/', $str)) {
765 $str = preg_replace(
'/^(\d{1,2})\.(\d{1,2})\.(\d{2}(\d{2})?)/',
'$3-$2-$1', $str);
769 if(preg_match(
'#^\d{1,2}/\d{1,2}/(\d{2}(\d{2})?)#', $str)) {
770 $str = preg_replace(
'#^(\d{1,2})/(\d{1,2})/(\d{2}(\d{2})?)#',
'$3-$1-$2', $str);
776 return (strtotime($str, $offset));
780 return strtotime($str);
789 public function detectPageLanguage() : array
792 $sTempCurrentPageLanguage = ($_GET[
"lang"] ?? NULL);
795 if (is_null($sTempCurrentPageLanguage))
797 $sTempCurrentPageLanguage = LEPTON_frontend::getInstance()->page[
'language'];
801 $sBasic = strtolower($sTempCurrentPageLanguage).
"_".strtoupper($sTempCurrentPageLanguage);
816 public function checkINTLExtensionExists() : bool
818 $aAllExtensions = get_loaded_extensions();
819 if (!in_array(
"intl", $aAllExtensions))
838 public function formatWithMySQL(
string $format = DEFAULT_DATE_FORMAT,
int|
null $timestamp =
null,
string $optionalLang =
"" ): string|null
840 if ($timestamp ===
null)
845 $sRealFormat = $this->CORE_date_formats_MYSQL[$format] ?? $format;
847 $tempLang = self::buildLanguageKey(
848 (empty($optionalLang)
856 if ($tempLang !==
"en_EN")
858 $database->simple_query(
"SET lc_time_names = '".$tempLang.
"';");
862 $database->simple_query(
"SET lc_time_names = 'en_gb';");
866 $sQuery = ($timestamp < 0)
867 ?
"SELECT DATE_FORMAT( DATE_ADD(FROM_UNIXTIME(0), interval " . $timestamp .
" second),'" . $sRealFormat .
"');"
868 :
"SELECT DATE_FORMAT( FROM_UNIXTIME(" . $timestamp .
"),'" . $sRealFormat .
"');"
883 static function buildLanguageKey(
string $key = LANGUAGE): string
885 if (preg_match(
"~^[a-z]{2}_[A-Z]{2}$~", $key)) {
888 } elseif (strlen($key) >= 2) {
890 $key = substr(trim($key), 0, 2);
891 return strtolower($key) .
"_" . strtoupper($key);
893 return "Error: need at least two chars for key! One given!";
941 public function getWeekdayNames(
string $sLanguage,
bool $bAbbreviated =
false): array
954 for ($i = 0; $i < 7; $i++)
956 $iTimestamp = mktime($stunde, $minute, $sekunde, $monat, $tag+$i, $jahr);
957 $returnValue[$i+1] = $this->formatWithMySQL(($bAbbreviated ?
"%a" :
"%W"), $iTimestamp, $sLanguage);
973 public function getMonthNames(
string $sLanguage,
bool $bAbbreviated =
false): array
986 for ($monat = 1; $monat <= 12; $monat++)
988 $iTimestamp = mktime($stunde, $minute, $sekunde, $monat, $tag, $jahr);
989 $returnValue[$monat] = $this->formatWithMySQL(($bAbbreviated ?
"%b" :
"%M"), $iTimestamp, $sLanguage);
995 public function getLocaleList(): array
997 return self::MYSQL_LOCALES;
1000########################################### will replace code from LEPTON_basics ###########################################
1010 static function get_dateformats(): array
1016 $actual_time = time();
1019 $sSystemDefault =
"";
1020 if (isset($user_time) && $user_time ===
true)
1022 $sSystemDefault = date(DEFAULT_DATE_FORMAT, $actual_time).
' (';
1023 $sSystemDefault .= ($TEXT[
'SYSTEM_DEFAULT'] ?? self::SYSTEM_DEFAULT_STR).
')';
1028 'system_default' => $sSystemDefault,
1029 'j.n.Y' => date(
'j.n.Y', $actual_time).
' (j.n.Y)',
1030 'm/d/Y' => date(
'm/d/Y', $actual_time).
' (M/D/Y)',
1031 'd/m/Y' => date(
'd/m/Y', $actual_time).
' (D/M/Y)',
1032 'm.d.Y' => date(
'm.d.Y', $actual_time).
' (M.D.Y)',
1033 'd.m.Y' => date(
'd.m.Y', $actual_time).
' (D.M.Y)',
1034 'm-d-Y' => date(
'm-d-Y', $actual_time).
' (M-D-Y)',
1035 'd-m-Y' => date(
'd-m-Y', $actual_time).
' (D-M-Y)',
1036 'Y-m-d' => date(
'Y-m-d', $actual_time).
' (Y-M-D)',
1037 'Y.m.d' => date(
'Y.m.d', $actual_time).
' (Y.M.D)',
1052 $oThis = self::getInstance();
1053 $oThis->set_core_language(DEFAULT_LANGUAGE);
1069 if (LANGUAGE ==
'EN')
1071 foreach ($aFormatList as &$format)
1073 $DATE_FORMATS[$format] = $oThis->formatWithMySQL($format, $actual_time);
1079 "/([0-9]{1,2})th/i",
1080 "/([0-9]{1,2})rd/i",
1081 "/([0-9]{1,2})st/i",
1084 foreach ($aFormatList as &$format)
1086 $sFormatedTime = $oThis->formatWithMySQL($format, $actual_time);
1087 if (str_contains($format,
"S"))
1089 $sFormatedTime = preg_replace(
1091 "$1".(LANGUAGE ==
"NL" ?
"" :
"."),
1095 $DATE_FORMATS[$format] = $sFormatedTime;
1099 return $DATE_FORMATS;
1108 static function get_timeformats(): array
1114 $actual_time = time();
1117 $sSystemDefault =
"";
1119 if ((isset($user_time)) && ($user_time ===
true))
1121 $sSystemDefault = date(DEFAULT_TIME_FORMAT, $actual_time).
' (';
1122 $sSystemDefault .= ($TEXT[
'SYSTEM_DEFAULT'] ?? self::SYSTEM_DEFAULT_STR).
')';
1126 'system_default' => $sSystemDefault,
1127 'H:i' => date(
'H:i', $actual_time),
1128 'H:i:s' => date(
'H:i:s', $actual_time),
1129 'g:i a' => date(
'g:i a', $actual_time),
1130 'g:i A' => date(
'g:i A', $actual_time)
1145 static function get_days(
string $lang=
"en_EN",
bool $abbr=
false): array
1147 return self::getInstance()->getWeekdayNames($lang, $abbr);
1172 static function get_months(
string $lang=
"en_EN",
bool $abbr=
false): array
1174 return self::getInstance()->getMonthNames($lang, $abbr);
1199 static function get_timezones(): array
1202 "Pacific/Kwajalein",
1205 "America/Anchorage",
1206 "America/Los_Angeles",
1208 "America/Mexico_City",
1212 "America/Buenos_Aires",
1213 "Atlantic/Reykjavik",
1217 "Europe/Kaliningrad",
1228 "Australia/Adelaide",
static getInstance(array &$settings=[])