Skip to content

Instantly share code, notes, and snippets.

@tannerhodges
Created November 25, 2015 15:40
Show Gist options
  • Save tannerhodges/21d635812deab28445c0 to your computer and use it in GitHub Desktop.
Save tannerhodges/21d635812deab28445c0 to your computer and use it in GitHub Desktop.
Display numbers with ordinal suffix, or optionally only the suffix.
/**
* 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