Created
July 27, 2022 03:12
-
-
Save bobbydank/2a92b10056ccd1d9dbcddebacef9d22f to your computer and use it in GitHub Desktop.
Simple function for turning a number into an ordinal string.
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 | |
/** | |
* Simple function for turning a number into an ordinal string. | |
* source: https://catswhocode.com/php-sanitize-input/ | |
*/ | |
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