Mar 2, 2009
Ordinal Suffixes in PHP
function ordinalize($int) {
//sanity
$int = (int)$int;
if(!is_numeric($int)) return false;
// You could internationalize here
$suffixes = array('th', 'st', 'nd', 'rd');
// Get the number in the tens place
$tens = substr($int,-2,1);
// All of the numbers 10-19 return 'th'
if($tens == 1) return $int.$suffixes[0];
// Get the number in the ones place
$cardinal = substr($int,-1);
// All numbers ending in 1, 2, 3 have special suffixes
if($suffixes[$cardinal]) return $int.$suffixes[$cardinal];
// All remaining numbers end in 'th'
return $int.$suffixes[0];
}



No Comments, Comment or Ping
Reply to “Ordinal Suffixes in PHP”