Posted By

Scooter on 05/19/08


Tagged


Versions (?)

Ordinal Numbers


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2008/04/ordinal-numbers.html

  1. <?php
  2. /*
  3.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. // Returns the ordinal of a cardinal number.
  12. function ordinal($cardinal)
  13. {
  14. if (substr($cardinal, -1, 1) == 1 && substr($cardinal, -2, 2) != 11)
  15. {
  16. return ($cardinal . 'st');
  17. }
  18. elseif (substr($cardinal, -1, 1) == 2 && substr($cardinal, -2, 2) != 12)
  19. {
  20. return ($cardinal . 'nd');
  21. }
  22. elseif (substr($cardinal, -1, 1) == 3 && substr($cardinal, -2, 2) != 13)
  23. {
  24. return ($cardinal . 'rd');
  25. }
  26. else
  27. {
  28. return ($cardinal . 'th');
  29. }
  30. }
  31.  
  32. // Unit test.
  33. //for($i = 0; $i < 25; $i++)
  34. //{
  35. // echo "<p>" . ordinal($i) . "</p>";
  36. //}
  37. ?>

Report this snippet  

You need to login to post a comment.