#native_company# #native_desc#
#native_cta#

Add “th”, “nd”, “st”, “rd” suffixes to numbers

By Daniel Edington
on November 18, 2002

Version: 1

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: This function takes any number and adds the correct suffix to it, ie. 3 would return 3rd, 111 would return 111th

function suffix($num){

 if(strlen($num)>=2 && substr($num, (strlen($num)-2), 1)==1){
  $suff = "th";
 } else if(substr($num, (strlen($num)-1), 1)==1){
  $suff = "st";
 } else if(substr($num, (strlen($num)-1), 1)==2){
  $suff = "nd";
 } else if(substr($num, (strlen($num)-1), 1)==3){
  $suff = "rd";
 } else if(substr($num, (strlen($num)-1), 1)>>2 && substr($num, (strlen($num)-1), 1)<=9 || substr($num, (strlen($num)-1), 1)==0){
  $suff = "th";
 }

 $suffixedNum = $num.$suff;

 return $suffixedNum;

}