Carnet Wiki

Format de temps relatif en français

Version 4 — Janvier 2021 JLuc

Voir
-  https://github.com/tc39/proposal-intl-relative-time
-  https://v8 http://cldr .unicode.org/
-  https://developers.
google.dev/features/intl-relativetimeformat com/web/updates/2018/10/intl-relativetimeformat

La fonction que j’utilise actuellement

Elle renvoie « il y a 3 minutes » ou « il y a 40 minutes » ou « il y a 2h30 » ou « ce matin » ou « ce midi » ou « hier » ou « avant hier » ou « 26 mars » si on passe ’affdate_court’ en 2e argument.

Le 2e argument est facultatif et vaut Et sinon , en général , c’est un nom de la fonction à appeler pour les dates antérieures à avant-hier.
Toutefois , si Si le 2e argument vaut ’duree_cache’, alors c’est la durée de validité de la réponse obtenue qu’on veut savoir, en secondes , avec une marge d’erreur acceptable convenable . Cette fonction peut ainsi être utilisée pour paramétrer la balise #CACHE étendue par le plugin cachelab.

//
function age_ilya_progressif ($from_datetime, $f_vieux=null) {
	$veut_cache = ($f_vieux=='duree_cache');


$from_time = strtotime($from_datetime);
	$fromH = intval(date('H', $from_time));  		// H : heure format 24h avec 0 devant si nec
	$from_dayyear = intval(date('z', $from_time));  // d : jour de l'année 0 à 365
	
	$to_time = strtotime($now=date("Y-m-d H:i:s"));
	$to_dayyear = intval (date ('z'));


$nuit =  ($fromH < 5);
	$matin = (($fromH>= 5) and ($fromH<12));
	$midi =  (($fromH>=12) and ($fromH<14));
	$aprem = (($fromH>=14) and ($fromH<18)); 
	$soir  = ($fromH>=18);
	$debug="";
//	spip_log ("de $from_datetime à $now : fromH $fromH nuit $nuit matin $matin midi $midi aprem $aprem soir $soir", "age_progressif");


$meme_jour = ($from_dayyear==$to_dayyear);
	$hier = ($from_dayyear+1 == $to_dayyear);
	$avanthier = ($from_dayyear+2 == $to_dayyear);
	
	$age_m = round(($to_time - $from_time) / 60, );


if ($age_m <=10) {
		if ($veut_cache)
			return 60;
		if ($age_m <= 2)
			return "à l'instant";
		// moins de 10 minute : pas d'arrondi
		return "il y a $age_m minutes";
	}


// moins d'une heure : on arondit à 5 minutes
	$age_m = round ($age_m/5)*5;
	$age_m_reste = $age_m % 60;
	$age_m_reste_long = sprintf("%02d", $age_m % 60); // %02d ou %'.02d : avec spécificateur de caractère de remplissage
	$age_h = round (($age_m-$age_m_reste)/60, );
//	spip_log ("de $from_datetime à $now : m=$age_m r=$age_m_reste rl=$age_m_reste_long h=$age_h", "age_progressif");
	if ($age_m < 60)  {
		if ($veut_cache)
			return 4*60;
		return "il y a $age_m minutes";
	}
	if ($age_h < 4) {
		if ($veut_cache)
			return 30*60;
		if ($age_m_reste)
			return  "il y a ${age_h}h$age_m_reste_long";
		return  "il y a ${age_h}h";
	}
	if ($meme_jour) {
		if ($veut_cache)
			return 60*60;
		if ($age_h < 6)
			return "il y a ${age_h}h";
		elseif ($nuit)
			return "la nuit dernière";
		elseif ($matin)
			return "ce matin";
		elseif ($midi)
			return "ce midi";
		elseif ($aprem)
			return "cet après-midi";	// jamais activé pour l'instant car on est pas le mm jour alors
		elseif ($soir)
			return "ce soir";			// idem
		spip_log ("age_ilya_progressif ($from_datetime,$f_vieux) : MEMEJOUR et age ~ $age_h h $age_m m mais ni cette nuit ni ce matin ni ce midi ni cet aprem ni ce soir", "assert_age_progressif");
		return "aujourd'hui";
	}
	if ($hier) {
		if ($veut_cache)
			return 6*60*60;
		if ($nuit or $matin)
			return "hier matin";
		elseif ($midi)
			return "hier midi";
		elseif ($aprem)
			return "hier après-midi";
		elseif ($soir)
			return "hier soir";
		spip_log ("age_ilya_progressif ($from_datetime,$f_vieux) : HIER et age ~ $age_h h $age_m m mais ni cette nuit ni ce matin ni ce midi ni cet aprem ni ce soir", "assert_age_progressif");
		return "hier";
	}
	if ($avanthier) {
		if ($veut_cache)
			return 10*60*60;
		return "avant-hier";
	}
	if ($veut_cache)
		return 24*60*60;
	if ($f_vieux and function_exists($f_vieux))
		return $f_vieux ($from_datetime);
	return "il y a ".round ($age_h/24,0)." jours.";
}