st, nd, rd, th to Number


/ Published in: PHP
Save to your folder(s)

Add st, nd, rd, th to any particular number


Copy this code and paste it in your HTML
  1. function ordinal($n) {
  2. $ln = (int) substr($n, -1);
  3. $sln = (int) substr($n, -2);
  4. $r = array('st','nd','rd');
  5. $es = (($sln < 11 || $sln > 19) && $ln > 0 && $ln < 4);
  6. return $n . ($es ? $r[$ln - 1] : 'th');
  7. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.