fix: ISO countdown date, IntlDateFormatter and UTF-8 truncation
This commit is contained in:
@@ -430,22 +430,62 @@ function formatDateISO8601($dateString) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tronque un texte à une longueur donnée
|
||||
* Formate une date en français (remplace strftime, déprécié depuis PHP 8.1)
|
||||
*
|
||||
* Utilise IntlDateFormatter quand l'extension intl est disponible,
|
||||
* sinon replie sur un formatage manuel (mois français en toutes lettres).
|
||||
*
|
||||
* @param DateTimeInterface $date Date à formater
|
||||
* @param bool $withTime true pour ajouter « à HH:mm »
|
||||
* @return string Date formatée, ex. « 11 octobre 2025 » ou « 11 octobre 2025 à 00:00 »
|
||||
*/
|
||||
function formatDateFr($date, $withTime = false) {
|
||||
if (class_exists('IntlDateFormatter')) {
|
||||
$formatter = new IntlDateFormatter(
|
||||
'fr_FR',
|
||||
IntlDateFormatter::FULL,
|
||||
IntlDateFormatter::FULL,
|
||||
$date->getTimezone(), // Le formateur ignore le fuseau du DateTime sans ça
|
||||
IntlDateFormatter::GREGORIAN,
|
||||
$withTime ? "d MMMM yyyy 'à' HH:mm" : 'd MMMM yyyy'
|
||||
);
|
||||
$formatted = $formatter->format($date);
|
||||
if ($formatted !== false) {
|
||||
return $formatted;
|
||||
}
|
||||
}
|
||||
|
||||
// Repli sans extension intl : mois français en toutes lettres
|
||||
$months = [
|
||||
1 => 'janvier', 'février', 'mars', 'avril', 'mai', 'juin',
|
||||
'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'
|
||||
];
|
||||
$formatted = $date->format('j') . ' ' . $months[(int) $date->format('n')] . ' ' . $date->format('Y');
|
||||
|
||||
if ($withTime) {
|
||||
$formatted .= ' à ' . $date->format('H:i');
|
||||
}
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tronque un texte à une longueur donnée (en caractères UTF-8)
|
||||
*
|
||||
* @param string $text Texte à tronquer
|
||||
* @param int $length Longueur maximale
|
||||
* @return string Texte tronqué
|
||||
*/
|
||||
function truncateText($text, $length = 200) {
|
||||
if (strlen($text) <= $length) {
|
||||
if (mb_strlen($text) <= $length) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
$truncated = substr($text, 0, $length);
|
||||
$lastSpace = strrpos($truncated, ' ');
|
||||
$truncated = mb_substr($text, 0, $length);
|
||||
$lastSpace = mb_strrpos($truncated, ' ');
|
||||
|
||||
if ($lastSpace !== false) {
|
||||
$truncated = substr($truncated, 0, $lastSpace);
|
||||
$truncated = mb_substr($truncated, 0, $lastSpace);
|
||||
}
|
||||
|
||||
return $truncated . '...';
|
||||
|
||||
Reference in New Issue
Block a user