Skip to content

Instantly share code, notes, and snippets.

@shmaltorhbooks
Last active December 18, 2015 00:59
Show Gist options
  • Select an option

  • Save shmaltorhbooks/5700526 to your computer and use it in GitHub Desktop.

Select an option

Save shmaltorhbooks/5700526 to your computer and use it in GitHub Desktop.
Add (th, st, nd, rd, th) to the end of a number
<?php
function ordinal($cdnl){
$test_c = abs($cdnl) % 10;
$ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th'
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $cdnl.$ext;
}
for($i=1;$i<100;$i++){
echo ordinal($i).'<br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment