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


/ Published in: PHP



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.