Created
December 21, 2011 16:11
-
-
Save danmatthews/1506569 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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