function declination($n = 0, $year)
{
$year = explode('|', $year);
$n = intval($n);
return $n % 10 == 1 && $n % 100 != 11 ? $year[0] : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? $year[1] : $year[2]);
}
echo '5 ' . declination(5, 'год|года|лет');
export function declOfNum(num, titles) {
const cases = [2, 0, 1, 1, 1, 2];
return titles[(num % 100 > 4 && num % 100 < 20)
? 2
: cases[(num % 10 < 5) ? num % 10 : 5]
];
}
declOfNum(3, ['день', 'дня', 'дней']) // дня
/**
* Returns plural form
* @param integer $n number of items
* @param string $one
* @param string $some
* @param string $many
* @example echo plural_form(42, 'арбуз', 'арбуза', 'арбузов');
*/
public static function pluralForm($n, $one, $some, $many) {
return $n%10==1&&$n%100!=11?$one:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$some:$many);
}