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 3 of 3
  1. #1
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add_ordinals in PHP

    http://www.codingforums.com/showthre...925#post178925

    A beautiful javascript function as the result of the best minds on this forum coming together, but I'm trying to rewrite it in PHP. Maybe because I don't fully understand the Javascript in it, but this function always seems to return 'th'.
    Code:
    function add_ordinals($num){
      $ords = array ( "th","st","nd","rd" );
      return 
        $num . $ords[(!((($num=this%10) >3) || (floor(this%100/10)==1))) * $num];
    }
    Any help in debugging is appreciated.
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how about this? (ain't english so not to fluent in ordinals)

    function toOrdinal($num)
    {
    $ords = array ( "st","nd","rd" ,"th");
    return $num .$ords[(($num%10<4)&&($num%10>0)&&(floor($num%100/10)!=1))?($num%10-1):3];
    }

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    a ord on numbers using date function

    PHP Code:
    <?php
    $a
    =array(1,2,3,4,5,6,7,8,22,21,101,31);
    foreach(
    $a as $b)
        {
        
    $ord=date("S",((substr($b,-1,1)*86400)-86400));
        echo 
    $b.$ord."<br>";
        }
    ?>


  •  

    Posting Permissions

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