Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Proper Ending for Numbers (i.e. 2nd, 3rd, 8th) (See related posts)

// Run numbers through this to add proper endings (i.e. 2nd, 3rd, 8th)


 function nth_num($age, $small = 0) {
        $last_char_age = substr("$age", -1);
        switch($last_char_age) {
                case '1' :
                        $th = 'st';
                        break;
                case '2' :
                        $th = 'nd';
                        break;
                case '3' :
                        $th = 'rd';
                        break;
                default :
                        $th = 'th';
                        break;
        }
        if ($age > 10 && $age < 20) $th = 'th';
        if (0 == $small) $niceage = $age.$th;
        if (1 == $small) $niceage = $age."$th";
        return $niceage;
        }


You need to create an account or log in to post comments to this site.


Related Posts