PHPBuilder - Add "th", "nd", "st", "rd" suffixes to numbers



RSS Twitter
Snippets Calendars Dates

Add "th", "nd", "st", "rd" suffixes to numbers

by: Daniel Edington
|
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;

}

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Daniel Edington

Comment:



Comment:

(Maximum characters: 1200). You have characters left.