_
_ Handy PHP
_
_
Tutorials, Scripts, Information And Other Resources arrow PHP Functions arrow Ordinal Suffix
Thursday, 24 July 2008
_

Refer A Friend using Revolution Money Exchange
Resource Menu
Handy PHP Store
Free Domain Name
PHP Functions
PHP Downloads
PHP Newsfeeds
PHP Tutorials
Webmaster Tools
User Login
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index
Handy PHP
Free PHP Help!


 
This Section Is Scheduled
For Maintenance

    The format of the PHP Functions section here at Handy PHP needs some attention soon.  Recent changes to the website have brought to light some significant layout problems with this section.  The need for a standard format for all functions has resulted in the problems.  Each function will be reformated, enhanced, archived, and updated as time allows. In the meantime, some screen resolutions may display a distorted version of the website.
Thank you for your visit and patience.

If you encounter any major problems with viewing the functions, please inform us via the forums.
If you are unable to find what you needed here, please let us know what you wanted in the forums so that we may add relevant content in the future.

Ordinal Suffix Print E-mail
User Rating: / 3
PoorBest 
PHP Resources - Handy PHP Functions
Written by M. L. Griswold   
Sunday, 03 September 2006
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. 



Discuss this article on the forums. (1 posts)
Last Updated ( Saturday, 10 May 2008 )
 
< Prev   Next >

_
 
_
_
© 2008 Handy PHP