Posted By

theworldofdan on 07/21/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


Add (th, st, nd, rd, th) to the end of a number


 / Published in: PHP
 

URL: http://phpsnips.com/snippet.php?id=37

  1. <?php
  2. function ordinal($cdnl){
  3. $test_c = abs($cdnl) % 10;
  4. $ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th'
  5. : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
  6. ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
  7. return $cdnl.$ext;
  8. }
  9. for($i=1;$i<100;$i++){
  10. echo ordinal($i).'<br>';
  11. }

Report this snippet  

You need to login to post a comment.