Skip to content

Instantly share code, notes, and snippets.

@danmatthews
Created December 21, 2011 16:11
Show Gist options
  • Save danmatthews/1506569 to your computer and use it in GitHub Desktop.
Save danmatthews/1506569 to your computer and use it in GitHub Desktop.
/**
* Add order suffix to numbers ex. 1st 2nd 3rd 4th 5th
*
* @param int the number to ordinalize
* @return string the ordinalized version of $number
* @link http://snipplr.com/view/4627/a-function-to-add-a-prefix-to-numbers-ex-1st-2nd-3rd-4th-5th/
*/
public static function ordinalize($number)
{
if ( ! is_numeric($number))
{
return $number;
}
if (in_array(($number % 100), range(11, 13)))
{
return $number . 'th';
}
else
{
switch ($number % 10)
{
case 1:
return $number . 'st';
break;
case 2:
return $number . 'nd';
break;
case 3:
return $number . 'rd';
break;
default:
return $number . 'th';
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment