diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-01-07 14:30:42 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-03-06 21:11:12 +0100 |
commit | 52b503105d389d1796698114573ff618b2ad34a2 (patch) | |
tree | d636098fe0864db98c175cd7dfda19a1c069eb82 /application | |
parent | 1255a42cfed9ce419962c6cf29181a66c7e22bb8 (diff) | |
download | Shaarli-52b503105d389d1796698114573ff618b2ad34a2.tar.gz Shaarli-52b503105d389d1796698114573ff618b2ad34a2.tar.zst Shaarli-52b503105d389d1796698114573ff618b2ad34a2.zip |
Improve datetime display
Use php-intl extension to display datetimes a bit more nicely, depending on the locale.
What changes:
* the day is no longer displayed
* day number and month are ordered according to the locale
* the timezone is more readable (UTC+1 instead of CET)
Diffstat (limited to 'application')
-rw-r--r-- | application/Utils.php | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/application/Utils.php b/application/Utils.php index 19fb7116..a936b09f 100644 --- a/application/Utils.php +++ b/application/Utils.php | |||
@@ -225,44 +225,46 @@ function autoLocale($headerLocale) | |||
225 | $encodings = ['utf8', 'UTF-8']; | 225 | $encodings = ['utf8', 'UTF-8']; |
226 | if (!empty($matches[2])) { | 226 | if (!empty($matches[2])) { |
227 | $second = [strtoupper($matches[2]), strtolower($matches[2])]; | 227 | $second = [strtoupper($matches[2]), strtolower($matches[2])]; |
228 | $attempts = arrays_combination([$first, $separators, $second, ['.'], $encodings]); | 228 | $attempts = cartesian_product_generator([$first, $separators, $second, ['.'], $encodings]); |
229 | } else { | 229 | } else { |
230 | $attempts = arrays_combination([$first, $separators, $first, ['.'], $encodings]); | 230 | $attempts = cartesian_product_generator([$first, $separators, $first, ['.'], $encodings]); |
231 | } | 231 | } |
232 | } | 232 | } |
233 | } | 233 | } |
234 | setlocale(LC_ALL, $attempts); | 234 | setlocale(LC_ALL, implode('implode', iterator_to_array($attempts))); |
235 | } | 235 | } |
236 | 236 | ||
237 | /** | 237 | /** |
238 | * Combine multiple arrays of string to get all possible strings. | 238 | * Build a Generator object representing the cartesian product from given $items. |
239 | * The order is important because this doesn't shuffle the entries. | ||
240 | * | 239 | * |
241 | * Example: | 240 | * Example: |
242 | * [['a'], ['b', 'c']] | 241 | * [['a'], ['b', 'c']] |
243 | * will generate: | 242 | * will generate: |
244 | * - ab | 243 | * [ |
245 | * - ac | 244 | * ['a', 'b'], |
246 | * | 245 | * ['a', 'c'], |
247 | * TODO PHP 5.6: use the `...` operator instead of an array of array. | 246 | * ] |
248 | * | 247 | * |
249 | * @param array $items array of array of string | 248 | * @param array $items array of array of string |
250 | * | 249 | * |
251 | * @return array Combined string from the input array. | 250 | * @return Generator representing the cartesian product of given array. |
251 | * | ||
252 | * @see https://en.wikipedia.org/wiki/Cartesian_product | ||
252 | */ | 253 | */ |
253 | function arrays_combination($items) | 254 | function cartesian_product_generator($items) |
254 | { | 255 | { |
255 | $out = ['']; | 256 | if (empty($items)) { |
256 | foreach ($items as $item) { | 257 | yield []; |
257 | $add = []; | 258 | } |
258 | foreach ($item as $element) { | 259 | $subArray = array_pop($items); |
259 | foreach ($out as $key => $existingEntry) { | 260 | if (empty($subArray)) { |
260 | $add[] = $existingEntry . $element; | 261 | return; |
261 | } | 262 | } |
263 | foreach (cartesian_product_generator($items) as $item) { | ||
264 | foreach ($subArray as $value) { | ||
265 | yield $item + [count($item) => $value]; | ||
262 | } | 266 | } |
263 | $out = $add; | ||
264 | } | 267 | } |
265 | return $out; | ||
266 | } | 268 | } |
267 | 269 | ||
268 | /** | 270 | /** |
@@ -303,3 +305,33 @@ function normalize_spaces($string) | |||
303 | { | 305 | { |
304 | return preg_replace('/\s{2,}/', ' ', trim($string)); | 306 | return preg_replace('/\s{2,}/', ' ', trim($string)); |
305 | } | 307 | } |
308 | |||
309 | /** | ||
310 | * Format the date according to the locale. | ||
311 | * | ||
312 | * Requires php-intl to display international datetimes, | ||
313 | * otherwise default format '%c' will be returned. | ||
314 | * | ||
315 | * @param DateTime $date to format. | ||
316 | * @param bool $intl Use international format if true. | ||
317 | * | ||
318 | * @return bool|string Formatted date, or false if the input is invalid. | ||
319 | */ | ||
320 | function format_date($date, $intl = true) | ||
321 | { | ||
322 | if (! $date instanceof DateTime) { | ||
323 | return false; | ||
324 | } | ||
325 | |||
326 | if (! $intl || ! class_exists('IntlDateFormatter')) { | ||
327 | return strftime('%c', $date->getTimestamp()); | ||
328 | } | ||
329 | |||
330 | $formatter = new IntlDateFormatter( | ||
331 | setlocale(LC_TIME, 0), | ||
332 | IntlDateFormatter::LONG, | ||
333 | IntlDateFormatter::LONG | ||
334 | ); | ||
335 | |||
336 | return $formatter->format($date); | ||
337 | } | ||