Created
May 12, 2016 18:58
-
-
Save Rudde/f88ab71ced795fdb5e6fe1062e3cfefc to your computer and use it in GitHub Desktop.
Take a number input output it with proper suffix like 1st, 2nd, 3rd, and so on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Written by Barand from phpfreaks.com */ | |
function numSuffix($n) { | |
$str = "$n"; | |
$t = $n > 9 ? substr($str,-2,1) : 0; | |
$u = substr($str,-1); | |
if ($t==1) return $str . 'th'; | |
else switch ($u) { | |
case 1: return $str . 'st'; | |
case 2: return $str . 'nd'; | |
case 3: return $str . 'rd'; | |
default: return $str . 'th'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment