Handy PHP
Free Web Hosting
Get a free month of web hosting and step-by-step website design tool.
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index

Refer A Friend using Revolution Money Exchange
Ordinal Suffix PDF Print E-mail
Written by Handy PHP Administrator   
Sunday, 03 September 2006 15:13
Description:
  string ordinal_suffix(int value[, bool int])
     
  This function will add the correct ordinal suffix to any number.  1st, 2nd, 3rd, 4th, etc...
   
 
Function:
  function ordinal_suffix($value, $sup = 0){
// Function written by Marcus L. Griswold (vujsa)
// Can be found at http://www.handyphp.com
// Do not remove this header!

    is_numeric($value) or trigger_error("<b>\"$value\"</b> is not a number!, The value must be a number in the function <b>ordinal_suffix()</b>", E_USER_ERROR);
    if(substr($value, -2, 2) == 11 || substr($value, -2, 2) == 12 || substr($value, -2, 2) == 13){
        $suffix = "th";
    }
    else if (substr($value, -1, 1) == 1){
        $suffix = "st";
    }
    else if (substr($value, -1, 1) == 2){
        $suffix = "nd";
    }
    else if (substr($value, -1, 1) == 3){
        $suffix = "rd";
    }
    else {
        $suffix = "th";
    }
    if($sup){
        $suffix = "<sup>" . $suffix . "</sup>";
    }
    return $value . $suffix;
}
   
 
Usage:
 

echo  ordinal_suffix(65413, 1);

echo  ordinal_suffix(65421);

     
Result:
 
			65413th
			
			65421st 
			
     
Notes:
 

Failure to use a valid NUMBER in the function will return an error message!

By setting the second parameter, the suffix will be outputted in superscript.  If left blank then the output will be normal. 



{mos_fb_discuss:3}
Last Updated on Saturday, 10 May 2008 09:05
 
Earn More with LinkConnector