Posted By

distinctdev on 05/30/08


Tagged

number convert position ranking place


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

distinctdev
brent-man
axthos
not_skeletor
AlastairDewar


Convert a number to a position / ranking


 / Published in: PHP
 

Convert an number to a position / ranking. 1 becomes 1st, 2 becomes 2nd, etc...

  1. function numberToPlace($number) {
  2.  
  3. if(!is_numeric($number)) return false;
  4. $number = intval($number);
  5. $lastNumber = substr($number, -1);
  6. $lastTwo = substr($number, -2);
  7.  
  8. $append = ($lastNumber == '1' && $lastTwo != 11) ? 'st' : (($lastNumber == '2' && $lastTwo != 12) ? 'nd' : (($lastNumber == '3' && $lastTwo != 13) ? 'rd' : 'th'));
  9.  
  10. return $number . $append;
  11. }

Report this snippet  

You need to login to post a comment.