Created
November 25, 2015 15:40
-
-
Save tannerhodges/21d635812deab28445c0 to your computer and use it in GitHub Desktop.
Display numbers with ordinal suffix, or optionally only the suffix.
This file contains hidden or 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
/** | |
* Display numbers with ordinal suffix, or optionally only the suffix (helpful | |
* for handling superscripts, etc). | |
* | |
* Based on http://stackoverflow.com/a/3110033/1786459 | |
* | |
* @param Integer $number | |
* @param Boolean $suffix_only | |
* @return String | |
*/ | |
function ordinal($number, $suffix_only = false) { | |
$ends = ['th','st','nd','rd','th','th','th','th','th','th']; | |
$ordinal = ''; | |
if ((($number % 100) >= 11) && (($number%100) <= 13)) { | |
$ordinal = 'th'; | |
} else { | |
$ordinal = $ends[$number % 10]; | |
} | |
if (!$suffix_only) { | |
$ordinal = $number . $ordinal; | |
} | |
return $ordinal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment