|
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 )
|