Jump to content

Easy ordinal number suffixes ?


Mahngiel

Recommended Posts

Hey Freaks,

 

Anybody have a shortcut to implement ordinal numbers (1st, 2nd, 3rd) suffixes (sp? suffices??) ?

 

My first thought was to throw a function at it:

<?php
    function suffixizer( $number = 0 ) {
        if( $number == 0 )
            return FALSE;
        elseif( substr($number, -1) == 1 )
            return $suffixed = $number . 'st';
        elseif( substr($number, -1) == 2 )
            return $suffixed = $number . 'nd';
        elseif( substr($number, -1) == 3 )
            return $suffixed = $number . 'rd';
        else
            return $suffixed = $number . 'th';
    }
?>

 

Any thoughts?

 

[edit] forgot about 'th'[/edit]

Link to comment
https://forums.phpfreaks.com/topic/262163-easy-ordinal-number-suffixes/
Share on other sites

  function ordSuffix($n) 
  
  {
      $str = "$n";
      $t = $n > 9 ? substr($str,-2,1) : 0;
      $u = substr($str,-1);
      if ($t==1) 
        return $str . 'th';
      else 
          switch ($u) 
          {
              case 1: return $str . 'st';
              case 2: return $str . 'nd';
              case 3: return $str . 'rd';
              default: return $str . 'th';
          }
  }
  
  echo ordSuffix(1).'<br>';
  echo ordSuffix(11).'<br>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.