## Posted By

flashmac on 08/23/10

# Make Position Number

/ Published in: PHP

If you want to transform an int number to a position number (ie. 1 = 1st, 6 = 6th etc..)

`function make_position(\$position) {	\$last = substr( \$position, -1 );	\$seclast = substr( \$position, -2, -1 );	if( \$last > 3 || \$last == 0 ) \$ext = 'th';	else if( \$last == 3 ) \$ext = 'rd';	else if( \$last == 2 ) \$ext = 'nd';	else \$ext = 'st';  	if( \$last == 1 && \$seclast == 1) \$ext = 'th';	if( \$last == 2 && \$seclast == 1) \$ext = 'th';	if( \$last == 3 && \$seclast == 1) \$ext = 'th';  	return \$position.\$ext;} eg. make_position(2); // 2ndeg. make_position(8); // 8theg. make_position(21); // 21steg. make_position(23); // 23rd`

## Comments

Subscribe to comments
Posted By: feeela on August 24, 2010

Alternative notion, viewer if-statements: function numerals(\$number) { if(strlen(\$number) > 1 && 1 == substr(\$number, -2, -1)) { \$lastDigit = 0; } else { \$lastDigit = substr(\$number, -1); }

``````    switch(\$lastDigit)
{
case 3:
\$numeralAppendix = 'rd';
break;
case 2:
\$numeralAppendix = 'nd';
break;
case 1:
\$numeralAppendix = 'st';
break;
default:
\$numeralAppendix = 'th';
break;
}
return \$number . \$numeralAppendix;
}
``````

You need to login to post a comment.