Login Latest Snippets Home My Account
Snippee! A personal code snippet manager... Store, share, organize! ...

Ordinal numbers

Published: 2011-08-11 18:36:23 - In: PHP

1 = 1st 2 = 2nd 3 = 3rd 4 = 4th ...

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>';
}