Monday, January 25, 2010

Return the ordinal suffix for a number

<?php
function getOrdinal($number)
{
 $digit = abs($number) % 10;
 $ord = 'th'; //DEFAULT ORDINAL SUFFIX
 $ord = ((abs($number) %100 < 21 && abs($number) %100 > 4) ? 'th' : (($digit < 4) ? ($digit < 3) ? ($digit < 2) ? ($digit < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
 return $ord;
}
?>