Posted By

misteroneill on 08/31/09


Tagged

numbers ordinals st nd rd th


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


Show Number Ordinals


 / Published in: PHP
 

URL: http://www.misteroneill.com

This function takes a number and appends an ordinal suffix. By default, it uses the HTML tag around the suffix, but passing a Boolean false for the second parameter will not.

  1. function show_ordinal($num, $html = true) {
  2. $the_num = (string) $num;
  3. $last_digit = substr($the_num, -1, 1);
  4. switch($last_digit) {
  5. case "1":
  6. $the_num .= ($html) ? '<sup>st</sup>' : 'st';
  7. break;
  8. case "2":
  9. $the_num .= ($html) ? '<sup>nd</sup>' : 'nd';
  10. break;
  11. case "3":
  12. $the_num .= ($html) ? '<sup>rd</sup>' : 'rd';
  13. break;
  14. default:
  15. $the_num .= ($html) ? '<sup>th</sup>' : 'th';
  16. }
  17. return $the_num;
  18. }

Report this snippet  

You need to login to post a comment.