PHP Function to Convert Number to Ordinal ie 1 - 1st, 2 - 2nd, 3 - 3rd
PHP Function to Convert Number to Ordinal
Home
Short:
/* ----------------------------------------------------------- convert number to its ordinal ie 1 => 1st, 2 => 2nd, etc ----------------------------------------------------------- */ function numberToOrdinal($num) { if (!in_array(($num % 100),array(11,12,13))){ switch ($num % 10) { // Handle 1st, 2nd, 3rd case 1: return $num.'st'; case 2: return $num.'nd'; case 3: return $num.'rd'; } } return $num.'th'; }
source code home