fixes #263 add i18n support to date formatting with IntlDateFormatter

Introduced a new format_date function that uses IntlDateFormatter for proper internationalization when available, falling back to the legacy implementation otherwise. The previous format_date function was renamed to format_date_legacy.
This commit is contained in:
Linty
2025-09-04 14:10:12 +02:00
parent aed52cc79a
commit c078cffb8b
+49 -5
View File
@@ -792,15 +792,14 @@ function str2DateTime($original, $format=null)
}
/**
* returns a formatted and localized date for display
* returns a formatted and localized date for display (LEGACY use format_date)
*
* @param int|string timestamp or datetime string
* @param array $show list of components displayed, default is ['day_name', 'day', 'month', 'year']
* THIS PARAMETER IS PLANNED TO CHANGE
* @param string $format input format respecting date() syntax
* @return string
*/
function format_date($original, $show=null, $format=null)
function format_date_legacy($original, $show=null, $format=null)
{
global $lang;
@@ -816,8 +815,6 @@ function format_date($original, $show=null, $format=null)
$show = array('day_name', 'day', 'month', 'year');
}
// TODO use IntlDateFormatter for proper i18n
$print = '';
if (in_array('day_name', $show))
$print.= $lang['day'][ $date->format('w') ].' ';
@@ -843,6 +840,53 @@ function format_date($original, $show=null, $format=null)
return trim($print);
}
/**
* returns a formatted and localized date for display
*
* @param int|string timestamp or datetime string
* @param array $show list of components displayed, default is ['day_name', 'day', 'month', 'year']
* THIS PARAMETER IS PLANNED TO CHANGE
* @param string $format input format respecting date() syntax
* @return string
* @since 16
*/
function format_date($original, $show=null, $format=null)
{
global $user;
$date = str2DateTime($original, $format);
if (!$date)
{
return l10n('N/A');
}
if ($show === null || $show === true)
{
$show = array('day_name', 'day', 'month', 'year');
}
// use IntlDateFormatter for proper i18n need pkg php-intl
if (class_exists('IntlDateFormatter')
and in_array('year', $show)
and in_array('month', $show)
)
{
$timeType = in_array('time', $show) ? IntlDateFormatter::MEDIUM : IntlDateFormatter::NONE;
$dateType = IntlDateFormatter::FULL;
if (!in_array('day_name', $show))
{
$dateType = IntlDateFormatter::LONG;
}
$fmt = new IntlDateFormatter($user['language'], $dateType, $timeType);
return $fmt->format($date);
}
return format_date_legacy($original, $show, $format);
}
/**
* Format a "From ... to ..." string from two dates
* @param string $from