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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts

    1'st' 2'nd 3'rd' etc

    I've searched PHP and it's more than likely a simple solution I just can't find. How do I turn a variable of 1 to 1st, 2 to 2nd etc...

    Thanks in advance!

  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,079
    Thanks
    3
    Thanked 506 Times in 494 Posts
    You can use the date() function with the "S" format parameter for 1-31. Beyond that you would need to create your own function.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  3. #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    9,249
    Thanks
    4
    Thanked 932 Times in 919 Posts
    switch(day) {
    1: 21: 31: day.='st'; break;
    2: 22: day.='nd'; break;
    3: 23: day.='rd'; break;
    default: day.='th';
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  4. #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Both those assume it's a date, here's one that doesn't:
    PHP Code:
    function nth($in

    if(
    strlen($in)>&& substr($in,-2,1)==1
    return 
    $in."th"
    switch(
    substr($in,-1,1)) 

    case 
    1
    return 
    $in."st"
    case 
    2
    return 
    $in."nd"
    case 
    3
    return 
    $in."rd"
    default: 
    return 
    $in."th"




 

Posting Permissions

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