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
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,131
    Thanks
    2
    Thanked 328 Times in 320 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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 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
    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
    •