Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts

    function to add st, nd, rd or th after a number.

    Greetings all

    Wrote a little snippet to add st, nd, rd or th after a number put in it.

    Useable for creating numbers like "1st, 11th, 254th" etc etc.

    PHP Code:
    function stnd ($value) {
        
    $fixes = array("th""st""nd""rd""th""th""th""th""th""th");
        
    $eleven substr($value, -2);
        
    $subject substr($value, -1);
        if (
    $eleven 11 || $eleven 13) {
            return 
    $value .""$fixes["$subject"];
        } elseif(
    $eleven 10 && $eleven 14) {
            return 
    $value."th";    
        }

    used by called the function like normal

    PHP Code:
    echo stnd(244);
    // Will output "244th" 
    Have a nice day all..

    -Klaus
    Last edited by Jilli; 12-05-2013 at 11:37 AM. Reason: Edited as i discovered an issue with 311th and similar. Should work now for all numbers.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •