note 10289 deleted from ref.strings by aidan
| From: | aidan@php.net | Date: | Thu, 01 Jul 2004 01:09:02 +0000 |
| Subject: | note 10289 deleted from ref.strings by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72218@lists.php.net to get a copy of this message | ||
Note Submitter: R.J.Vrijhof@bigfoot.com
----
I wanted to convert numbers to an adjective form (so from "1" to "1st", from
"352" to "352nd", etc.), but couldn't find it in PHP, so I wrote this
little code to do that for me. Hope you find it useful:
function nth($in) {
if (!is_long($in))
return(-1);
switch ($in) {
case 0 : return("0th");
case 1 : return("1st");
case 2 : return("2nd");
case 3 : return("3rd");
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 10 :
case 11 :
case 12 :
case 13 :
case 14 :
case 15 :
case 16 :
case 17 :
case 18 :
case 19 : return($in."th");
}
switch (substr($in, -1, 1)) {
case 0 : return($in."th");
case 1 : return($in."st");
case 2 : return($in."nd");
case 3 : return($in."rd");
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 : return($in."th");
default : return(0);
}
}
It will return -1 when the input is not an integer and 0 when it couldn't find a match
(although that shouldn't happen).