Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Функция склонения существительных

Evgeny Mikulich edited this page Aug 31, 2018 · 2 revisions
<?php 
function plural_form($n, $forms) {
  return $n%10==1&&$n%100!=11?$forms[0]:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$forms[1]:$forms[2]);
}
//usage
$ends = [
   "банан", 
   "банана", 
   "бананов" 
]; 
plural_form(1, $ends); //банан
plural_form(2, $ends); //банана
plural_form(5, $ends); //бананов

Вариант от битрикса

use Bitrix\Main\Grid\Declension;
$yearDeclension = new Declension('год', 'года', 'лет');
$yearDeclension->get($year);
Clone this wiki locally