Answer the question
In order to leave comments, you need to log in
How to solve the problem (year|years|years) with one line of code?
You need to write a function that has an integer number of years at the input, and at the output a string corresponding to the rules of the Russian language, for example, "21 years old", "24 years old", "18 years old". The key point is that the function works on the maximum possible range of years.
Of course, type options
switch ($cnt) {
case 1:
case 21:
case 31:
case 41:
return $cnt.' год';
break;
case 2:
case 3:
case 4:
case 22:
case 23:
case 24:
case 32:
case 33:
case 34:
return $cnt.' года';
break;
default:
return $cnt.' лет';
}
andif( in_array($cnt, array(1, 21, 31, 41, 51, 61, 71, 81, 91)) ) return $cnt.' год';
elseif( in_array($cnt, array(2, 3, 4, 22, 23, 24, 32, 33, 34, 42, 43, 44, 52, 53, 54, 62, 63, 64, 72, 73, 74, 82, 83, 84, 92, 93, 94)) ) return $cnt.' годa';
else return $cnt.' лет';
Answer the question
In order to leave comments, you need to log in
return "$n " . ( ($n / 10) % 10 == 1 || $n % 10 > 4 || $n % 10 == 0 ? 'лет' : ($n % 10 == 1 ? 'год' : 'года') );
Well, take your own first option, but instead of comparing an integer, compare only the two least significant digits of the number)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question