A function to add a prefix to numbers ex. 1st 2nd 3rd 4th 5th


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



Copy this code and paste it in your HTML
  1. function ordinalize($number) {
  2. if (in_array(($number % 100),range(11,13))){
  3. return $number.'th';
  4. }else{
  5. switch (($number % 10)) {
  6. case 1:
  7. return $number.'st';
  8. break;
  9. case 2:
  10. return $number.'nd';
  11. break;
  12. case 3:
  13. return $number.'rd';
  14. default:
  15. return $number.'th';
  16. break;
  17. }
  18. }
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.