3declare(strict_types=1);
24 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'
128 public array $CORE_time_formats_PHP = [
129 'g:i A' =>
'%I:%M %p',
130 'g:i a' =>
'%I:%M %P',
131 'H:i:s' =>
'%H:%M:%S',
142 public array $CORE_time_formats_MYSQL = [
143 'g:i A' =>
'%l:%i %p',
145 'H:i:s' =>
'%H:%i:%s',
156 public array $CORE_date_formats_DatePicker = [
157 'l, jS F, Y'=>
'DD, d. MM yy',
158 'jS F, Y' =>
'e B, yy',
161 'D M d, Y' =>
'DD d MM, yy',
181 public bool $useINTL =
false;
182 public bool $intl_installed =
false;
191 public array $CORE_date_formats_INTL = [
192 'l, jS F, Y'=>
'A, e B, yyyy',
193 'jS F, Y' =>
'e B, yyyy',
194 'd M Y' =>
'd M yyyy',
195 'M d Y' =>
'M d yyyy',
196 'D M d, Y' =>
'EE d MM, yyyy',
203 'j.n.Y' =>
'd.M.yyyy'
210 public const MYSQL_LOCALES = [
211 "ar_AE" =>
"Arabic - United Arab Emirates",
212 "ar_BH" =>
"Arabic - Bahrain",
213 "ar_DZ" =>
"Arabic - Algeria",
214 "ar_EG" =>
"Arabic - Egypt",
215 "ar_IN" =>
"Arabic - India",
216 "ar_IQ" =>
"Arabic - Iraq",
217 "ar_JO" =>
"Arabic - Jordan",
218 "ar_KW" =>
"Arabic - Kuwait",
219 "ar_LB" =>
"Arabic - Lebanon",
220 "ar_LY" =>
"Arabic - Libya",
221 "ar_MA" =>
"Arabic - Morocco",
222 "ar_OM" =>
"Arabic - Oman",
223 "ar_QA" =>
"Arabic - Qatar",
224 "ar_SA" =>
"Arabic - Saudi Arabia",
225 "ar_SD" =>
"Arabic - Sudan",
226 "ar_SY" =>
"Arabic - Syria",
227 "ar_TN" =>
"Arabic - Tunisia",
228 "ar_YE" =>
"Arabic - Yemen",
229 "be_BY" =>
"Belarusian - Belarus",
230 "bg_BG" =>
"Bulgarian - Bulgaria",
231 "ca_ES" =>
"Catalan - Spain",
232 "cs_CZ" =>
"Czech - Czech Republic",
233 "da_DK" =>
"Danish - Denmark",
234 "de_AT" =>
"German - Austria",
235 "de_BE" =>
"German - Belgium",
236 "de_CH" =>
"German - Switzerland",
237 "de_DE" =>
"German - Germany",
238 "de_LU" =>
"German - Luxembourg",
239 "el_GR" =>
"Greek - Greece",
240 "en_AU" =>
"English - Australia",
241 "en_CA" =>
"English - Canada",
242 "en_GB" =>
"English - United Kingdom",
243 "en_IN" =>
"English - India",
244 "en_NZ" =>
"English - New Zealand",
245 "en_PH" =>
"English - Philippines",
246 "en_US" =>
"English - United States",
247 "en_ZA" =>
"English - South Africa",
248 "en_ZW" =>
"English - Zimbabwe",
249 "es_AR" =>
"Spanish - Argentina",
250 "es_BO" =>
"Spanish - Bolivia",
251 "es_CL" =>
"Spanish - Chile",
252 "es_CO" =>
"Spanish - Colombia",
253 "es_CR" =>
"Spanish - Costa Rica",
254 "es_DO" =>
"Spanish - Dominican Republic",
255 "es_EC" =>
"Spanish - Ecuador",
256 "es_ES" =>
"Spanish - Spain",
257 "es_GT" =>
"Spanish - Guatemala",
258 "es_HN" =>
"Spanish - Honduras",
259 "es_MX" =>
"Spanish - Mexico",
260 "es_NI" =>
"Spanish - Nicaragua",
261 "es_PA" =>
"Spanish - Panama",
262 "es_PE" =>
"Spanish - Peru",
263 "es_PR" =>
"Spanish - Puerto Rico",
264 "es_PY" =>
"Spanish - Paraguay",
265 "es_SV" =>
"Spanish - El Salvador",
266 "es_US" =>
"Spanish - United States",
267 "es_UY" =>
"Spanish - Uruguay",
268 "es_VE" =>
"Spanish - Venezuela",
269 "et_EE" =>
"Estonian - Estonia",
270 "eu_ES" =>
"Basque - Spain",
271 "fi_FI" =>
"Finnish - Finland",
272 "fo_FO" =>
"Faroese - Faroe Islands",
273 "fr_BE" =>
"French - Belgium",
274 "fr_CA" =>
"French - Canada",
275 "fr_CH" =>
"French - Switzerland",
276 "fr_FR" =>
"French - France",
277 "fr_LU" =>
"French - Luxembourg",
278 "gl_ES" =>
"Galician - Spain",
279 "gu_IN" =>
"Gujarati - India",
280 "he_IL" =>
"Hebrew - Israel",
281 "hi_IN" =>
"Hindi - India",
282 "hr_HR" =>
"Croatian - Croatia",
283 "hu_HU" =>
"Hungarian - Hungary",
284 "id_ID" =>
"Indonesian - Indonesia",
285 "is_IS" =>
"Icelandic - Iceland",
286 "it_CH" =>
"Italian - Switzerland",
287 "it_IT" =>
"Italian - Italy",
288 "ja_JP" =>
"Japanese - Japan",
289 "ko_KR" =>
"Korean - Republic of Korea",
290 "lt_LT" =>
"Lithuanian - Lithuania",
291 "lv_LV" =>
"Latvian - Latvia",
292 "mk_MK" =>
"Macedonian - North Macedonia",
293 "mn_MN" =>
"Mongolia - Mongolian",
294 "ms_MY" =>
"Malay - Malaysia",
295 "nb_NO" =>
"Norwegian(Bokmål) - Norway",
296 "nl_BE" =>
"Dutch - Belgium",
297 "nl_NL" =>
"Dutch - The Netherlands",
298 "no_NO" =>
"Norwegian - Norway",
299 "pl_PL" =>
"Polish - Poland",
300 "pt_BR" =>
"Portugese - Brazil",
301 "pt_PT" =>
"Portugese - Portugal",
302 "rm_CH" =>
"Romansh - Switzerland",
303 "ro_RO" =>
"Romanian - Romania",
304 "ru_RU" =>
"Russian - Russia",
305 "ru_UA" =>
"Russian - Ukraine",
306 "sk_SK" =>
"Slovak - Slovakia",
307 "sl_SI" =>
"Slovenian - Slovenia",
308 "sq_AL" =>
"Albanian - Albania",
309 "sr_RS" =>
"Serbian - Serbia",
310 "sv_FI" =>
"Swedish - Finland",
311 "sv_SE" =>
"Swedish - Sweden",
312 "ta_IN" =>
"Tamil - India",
313 "te_IN" =>
"Telugu - India",
314 "th_TH" =>
"Thai - Thailand",
315 "tr_TR" =>
"Turkish - Turkey",
316 "uk_UA" =>
"Ukrainian - Ukraine",
317 "ur_PK" =>
"Urdu - Pakistan",
318 "vi_VN" =>
"Vietnamese - Vietnam",
319 "zh_CN" =>
"Chinese - China",
320 "zh_HK" =>
"Chinese - Hong Kong",
321 "zh_TW" =>
"Chinese - Taiwan"
331 public static function getInstance(
bool $bUseINTL =
false): object
333 if (
null === static::$instance) {
334 static::$instance =
new static();
336 static::$instance->useINTL = $bUseINTL;
338 if (
true === $bUseINTL)
340 static::$instance->intl_installed = static::$instance->checkINTLExtensionExists();
343 return static::$instance;
349 protected function __construct ()
360 public function addLanguage ($aString=
""): bool
362 if ($aString ==
"") {
365 if (
false === in_array($aString, $this->lang)) {
366 $this->lang[] = $aString;
377 public function setFormat(
string $aString=
"" ): bool
381 $this->sINTLFormat = $aString;
383 $this->format=$aString;
399 public function toHTML(
int $aTimestamp = 0 ): string
401 if (NULL == $aTimestamp)
403 $aTimestamp = TIME();
406 $aTempLocale = setlocale(LC_ALL, $this->lang);
407 if (
false === $aTempLocale)
409 $aTempLocale = setlocale(LC_ALL, 0);
412 if ((
true === $this->useINTL) && (
true === $this->intl_installed))
414 $fmt = datefmt_create(
416 IntlDateFormatter::FULL,
417 IntlDateFormatter::FULL,
418 DEFAULT_TIMEZONE_STRING,
419 IntlDateFormatter::GREGORIAN,
426 $this->useINTL =
false;
427 return date($this->format, $aTimestamp);
430 return datefmt_format($fmt, $aTimestamp );
438 if (class_exists(
"IntlDateFormatter"))
440 $oTempFormatter =
new IntlDateFormatter(
442 IntlDateFormatter::LONG,
443 IntlDateFormatter::NONE,
444 DEFAULT_TIMEZONE_STRING,
445 IntlDateFormatter::GREGORIAN,
448 return $oTempFormatter->format($aTimestamp);
452 return date(str_replace(
"%",
"",$this->format), $aTimestamp);
467 public function setLanguage(array $aArray = []): bool
470 foreach ($aArray as $a)
494 public function transform (
string $aDateString =
"01.01.1971",
string $aFormat=
"dmy"): string
496 $this->_force_date ($aDateString);
497 $this->_force_format ($aFormat);
499 $temp = explode(
".", $aDateString);
509 $this->_force_year($temp[2]);
510 $temp_time = mktime( 1, 0, 0, $temp[1], $temp[0], $temp[2]);
514 $this->_force_year($temp[2]);
515 $temp_time = mktime( 1, 0, 0, $temp[0], $temp[1], $temp[2]);
519 $this->_force_year($temp[0]);
520 $temp_time = mktime( 1, 0, 0, $temp[1], $temp[2], $temp[0]);
534 return $this->toHTML($temp_time);
552 private function _force_date(
string &$aDateString ): void
554 $pattern = [
"*[\\/|.|-]+*"];
557 $aDateString = preg_replace($pattern, $replace, $aDateString);
571 private function _force_format(
string &$aFormat): void
574 $aFormat = strtolower ($aFormat);
576 $pattern = [
"*[\\/|.|-]+*",
"*[ |%]+*"];
579 $aFormat = preg_replace($pattern, $replace, $aFormat);
591 private function _force_year(
string &$aYearStr =
"1971"): void
593 if (strlen($aYearStr) == 2)
595 $aYearStr = (((int) $aYearStr > $this->force_year + (int) DATE(
"y", TIME())) ?
"19" :
"20").$aYearStr;
597 if (strlen($aYearStr) > 4)
599 $aYearStr = substr($aYearStr, 0, 4);
610 public function parse_string (
string &$aStr =
"",
string $aPattern =
"/([0-3][0-9].[01]{0,1}[0-9].[0-9]{2,4})/s"): void
613 preg_match_all($aPattern, $aStr, $found );
614 foreach ($found[1] as $a)
616 $aStr = str_replace($a, $this->transform($a), $aStr);
628 public function set_core_language(
string $aKeyStr =
""): bool
631 $return_value =
true;
637 $this->lang = [
'de_DE.UTF-8',
'de_DE@euro',
'de_DE',
'de',
'ge'];
641 $this->lang = [
'en_EN@euro',
'en_EN',
'en',
'EN',
'en_UK',
'UK',
'en_US',
'en_GB',
'en_CA'];
645 $this->lang = [
'FR',
'fr_FR.UTF-8',
'fr_FR',
'fr_FR@euro',
'fr'];
649 $this->lang = [
'it_IT@euro',
'it_IT',
'it'];
653 $this->lang = [
'nl_NL@euro',
'nl_NL',
'nl',
'Dutch',
'nld_nld'];
657 $this->lang = [
'RU',
'ru_RU.UTF-8',
'ru_RU',
'ru_RU@euro',
'ru'];
661 $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'];
665 $this->test_locale($aKeyStr);
668 return $return_value;
685 public function test_locale (
string $aKey =
"de_DE",
bool $use_it =
true): array
687 if (strlen($aKey) == 2)
689 $aKey = strtolower($aKey).
"_".strtoupper($aKey);
694 exec(
'locale -a', $temp_array);
698 foreach($temp_array as $lang_key)
700 if (substr($lang_key, 0,5) == $aKey)
706 if (!empty($all) && (
true === $use_it))
720 public function formatToDatepicker(
string $sFormatString =
""): string
722 if (isset($this->CORE_date_formats_DatePicker[$sFormatString])) {
723 return $this->CORE_date_formats_DatePicker[$sFormatString];
724 } elseif (isset($this->CORE_date_formats_DatePicker[DATE_FORMAT])) {
725 return $this->CORE_date_formats_DatePicker[DATE_FORMAT];
739 public function calendarToTimestamp(
string $str =
"",
int $offset = 0): int
742 if ($str ==
'0' || $str ==
'')
749 if(preg_match(
'/^\d{1,2}\.\d{1,2}\.\d{2}(\d{2})?/', $str)) {
750 $str = preg_replace(
'/^(\d{1,2})\.(\d{1,2})\.(\d{2}(\d{2})?)/',
'$3-$2-$1', $str);
754 if(preg_match(
'#^\d{1,2}/\d{1,2}/(\d{2}(\d{2})?)#', $str)) {
755 $str = preg_replace(
'#^(\d{1,2})/(\d{1,2})/(\d{2}(\d{2})?)#',
'$3-$1-$2', $str);
761 return (strtotime($str, $offset));
765 return (strtotime($str));
774 public function detectPageLanguage() : array
777 $sTempCurrentPageLanguage = ($_GET[
"lang"] ?? NULL);
780 if (is_null($sTempCurrentPageLanguage))
782 $sTempCurrentPageLanguage = LEPTON_frontend::getInstance()->page[
'language'];
786 $sBasic = strtolower($sTempCurrentPageLanguage).
"_".strtoupper($sTempCurrentPageLanguage);
801 public function checkINTLExtensionExists() : bool
803 $aAllExtensions = get_loaded_extensions();
804 if (!in_array(
"intl", $aAllExtensions))
823 public function formatWithMySQL(
string $format = DEFAULT_DATE_FORMAT,
int|
null $timestamp =
null,
string $optionalLang =
"" ): string|null
825 if ($timestamp ===
null)
830 $sRealFormat = $this->CORE_date_formats_MYSQL[$format] ?? $format;
832 $tempLang = self::buildLanguageKey(
833 (empty($optionalLang)
841 if ($tempLang !==
"en_EN")
843 $database->simple_query(
"SET lc_time_names = '".$tempLang.
"';");
847 $database->simple_query(
"SET lc_time_names = 'en_gb';");
851 $sQuery = ($timestamp < 0)
852 ?
"SELECT DATE_FORMAT( DATE_ADD(FROM_UNIXTIME(0), interval " . $timestamp .
" second),'" . $sRealFormat .
"');"
853 :
"SELECT DATE_FORMAT( FROM_UNIXTIME(" . $timestamp .
"),'" . $sRealFormat .
"');"
868 static function buildLanguageKey(
string $key = LANGUAGE): string
870 if (preg_match(
"~^[a-z]{2}_[A-Z]{2}$~", $key)) {
873 } elseif (strlen($key) >= 2) {
875 $key = substr(trim($key), 0, 2);
876 return strtolower($key) .
"_" . strtoupper($key);
878 return "Error: need at least two chars for key! One given!";
926 public function getWeekdayNames(
string $sLanguage,
bool $bAbbreviated =
false): array
939 for ($i = 0; $i < 7; $i++)
941 $iTimestamp = mktime($stunde, $minute, $sekunde, $monat, $tag+$i, $jahr);
942 $returnValue[$i+1] = $this->formatWithMySQL(($bAbbreviated ?
"%a" :
"%W"), $iTimestamp, $sLanguage);
958 public function getMonthNames(
string $sLanguage,
bool $bAbbreviated =
false): array
971 for ($monat = 1; $monat <= 12; $monat++)
973 $iTimestamp = mktime($stunde, $minute, $sekunde, $monat, $tag, $jahr);
974 $returnValue[$monat] = $this->formatWithMySQL(($bAbbreviated ?
"%b" :
"%M"), $iTimestamp, $sLanguage);
980 public function getLocaleList(): array
982 return self::MYSQL_LOCALES;
985########################################### will replace code from LEPTON_basics ###########################################
995 static function get_dateformats(): array
1001 $actual_time = time();
1004 $sSystemDefault =
"";
1005 if (isset($user_time) && $user_time ===
true)
1007 $sSystemDefault = date(DEFAULT_DATE_FORMAT, $actual_time).
' (';
1008 $sSystemDefault .= ($TEXT[
'SYSTEM_DEFAULT'] ?? self::SYSTEM_DEFAULT_STR).
')';
1013 'system_default' => $sSystemDefault,
1014 'j.n.Y' => date(
'j.n.Y', $actual_time).
' (j.n.Y)',
1015 'm/d/Y' => date(
'm/d/Y', $actual_time).
' (M/D/Y)',
1016 'd/m/Y' => date(
'd/m/Y', $actual_time).
' (D/M/Y)',
1017 'm.d.Y' => date(
'm.d.Y', $actual_time).
' (M.D.Y)',
1018 'd.m.Y' => date(
'd.m.Y', $actual_time).
' (D.M.Y)',
1019 'm-d-Y' => date(
'm-d-Y', $actual_time).
' (M-D-Y)',
1020 'd-m-Y' => date(
'd-m-Y', $actual_time).
' (D-M-Y)'
1034 $oThis = self::getInstance();
1035 $oThis->set_core_language(DEFAULT_LANGUAGE);
1051 foreach ($aFormatList as &$format)
1053 $DATE_FORMATS[$format] = $oThis->formatWithMySQL($format, $actual_time);
1056 return $DATE_FORMATS;
1065 static function get_timeformats(): array
1071 $actual_time = time();
1074 $sSystemDefault =
"";
1076 if ((isset($user_time)) && ($user_time ===
true))
1078 $sSystemDefault = date(DEFAULT_TIME_FORMAT, $actual_time).
' (';
1079 $sSystemDefault .= ($TEXT[
'SYSTEM_DEFAULT'] ?? self::SYSTEM_DEFAULT_STR).
')';
1083 'system_default' => $sSystemDefault,
1084 'H:i' => date(
'H:i', $actual_time),
1085 'H:i:s' => date(
'H:i:s', $actual_time),
1086 'g:i a' => date(
'g:i a', $actual_time),
1087 'g:i A' => date(
'g:i A', $actual_time)
1102 static function get_days(
string $lang=
"en_EN",
bool $abbr=
false): array
1104 return self::getInstance()->getWeekdayNames($lang, $abbr);
1129 static function get_months(
string $lang=
"en_EN",
bool $abbr=
false): array
1131 return self::getInstance()->getMonthNames($lang, $abbr);
1156 static function get_timezones(): array
1159 "Pacific/Kwajalein",
1162 "America/Anchorage",
1163 "America/Los_Angeles",
1165 "America/Mexico_City",
1169 "America/Buenos_Aires",
1170 "Atlantic/Reykjavik",
1174 "Europe/Kaliningrad",
1185 "Australia/Adelaide",
static getInstance(array &$settings=[])