Elgg  Version 4.3
DateTime.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\I18n;
4 
5 use \DateTime as PHPDateTime;
6 
12 class DateTime extends PHPDateTime {
13 
22  public function formatLocale(string $format, string $language = null) {
23  if (!isset($language)) {
24  $language = _elgg_services()->translator->getCurrentLanguage();
25  }
26 
27  if (extension_loaded('intl')) {
28  $result = $this->formatIntl($format, $language);
29  } else {
30  $result = $this->formatStrftime($format, $language);
31  }
32 
33  if ($result === false) {
34  elgg_log("Unable to generate locale representation for format: '{$format}', using non-locale version", 'INFO');
35  return $this->format($format);
36  }
37 
38  return $result;
39  }
40 
54  protected function dateFormatToStrftime(string $dateFormat) {
55  if (preg_match('/(?<!\\|%)[ntLBueIPZcr]/', $dateFormat)) {
56  // unsupported characters found
57  return false;
58  }
59 
60  $caracs = [
61  // Day - no strf eq : S
62  'd' => '%d', 'D' => '%a', 'j' => '%e', 'l' => '%A', 'N' => '%u', 'w' => '%w', 'z' => '%j',
63  // Week - no date eq : %U, %W
64  'W' => '%V',
65  // Month - no strf eq : n, t
66  'F' => '%B', 'm' => '%m', 'M' => '%b',
67  // Year - no strf eq : L; no date eq : %C, %g
68  'o' => '%G', 'Y' => '%Y', 'y' => '%y',
69  // Time - no strf eq : B, G, u; no date eq : %r, %R, %T, %X
70  'a' => '%P', 'A' => '%p', 'g' => '%l', 'h' => '%I', 'H' => '%H', 'i' => '%M', 's' => '%S',
71  // Timezone - no strf eq : e, I, P, Z
72  'O' => '%z', 'T' => '%Z',
73  // Full Date / Time - no strf eq : c, r; no date eq : %c, %D, %F, %x
74  'U' => '%s',
75  // less supported replacements
76  // Day
77  'S' => '',
78  // Time
79  'G' => '%k',
80  ];
81 
82  return strtr((string) $dateFormat, $caracs);
83  }
84 
94  protected function formatStrftime(string $format, string $language) {
95  // convert date() format to strftime() format
96  $correct_format = $this->dateFormatToStrftime($format);
97  if ($correct_format === false) {
98  return false;
99  }
100 
101  if (version_compare(PHP_VERSION, '8.1.0', '>=')) {
102  elgg_log('In order to get rid of the deprecated warnings about strftime() enable the "intl" PHP module', 'WARNING');
103  }
104 
105  // switch locale
106  $current_locale = _elgg_services()->locale->setLocaleFromLanguageKey(LC_TIME, $language);
107 
108  $result = strftime($correct_format, $this->getTimestamp());
109 
110  // restore locale
111  _elgg_services()->locale->setLocale(LC_TIME, $current_locale);
112 
113  return $result;
114  }
115 
125  protected function dateFormatToICU(string $dateFormat) {
126  if (preg_match('/(?<!\\|%)[BcILrtuUVZ]/', $dateFormat)) {
127  // unsupported characters found
128  return false;
129  }
130 
131  $caracs = [
132  // Day
133  'd' => 'dd', 'D' => 'EEE', 'j' => 'd', 'l' => 'EEEE', 'N' => 'e', 'w' => 'c', 'z' => 'D',
134  // Week
135  'W' => 'w',
136  // Month
137  'F' => 'MMMM', 'm' => 'MM', 'M' => 'MMM', 'n' => 'M',
138  // Year
139  'o' => 'Y', 'Y' => 'yyyy', 'y' => 'yy',
140  // Time
141  'a' => 'a', 'A' => 'a', 'g' => 'h', 'G' => 'H', 'h' => 'hh', 'H' => 'HH', 'i' => 'mm', 's' => 'ss',
142  // Timezone
143  'e' => 'VV', 'O' => 'xx', 'P' => 'xxx', 'p' => 'XXX', 'T' => 'zzz',
144  // less supported replacements
145  // Day
146  'S' => '',
147  ];
148 
149  return strtr((string) $dateFormat, $caracs);
150  }
151 
161  protected function formatIntl(string $format, string $language) {
162  $correct_format = $this->dateFormatToICU($format);
163  if ($correct_format === false) {
164  return false;
165  }
166 
167  $locale_for_language = _elgg_services()->locale->getLocaleForLanguage($language);
168 
169  $locale = new \IntlDateFormatter(elgg_extract(0, $locale_for_language, $language), \IntlDateFormatter::FULL, \IntlDateFormatter::FULL);
170  $locale->setPattern($correct_format);
171 
172  return $locale->format($this);
173  }
174 }
$format
Definition: date.php:39
formatIntl(string $format, string $language)
Try to format to a locale using the &#39;intl&#39; PHP module.
Definition: DateTime.php:161
dateFormatToICU(string $dateFormat)
Convert a date format to a ICU format.
Definition: DateTime.php:125
$language
Definition: useradd.php:19
formatLocale(string $format, string $language=null)
Try to format the date using locale output.
Definition: DateTime.php:22
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:399
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
formatStrftime(string $format, string $language)
Try to format to a locale using strftime()
Definition: DateTime.php:94
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
dateFormatToStrftime(string $dateFormat)
Convert a date format to a strftime format.
Definition: DateTime.php:54