Skip to content

Instantly share code, notes, and snippets.

@codemaker38
Created December 21, 2018 05:45
Show Gist options
  • Save codemaker38/18d359380b52b3c173de47b484f14724 to your computer and use it in GitHub Desktop.
Save codemaker38/18d359380b52b3c173de47b484f14724 to your computer and use it in GitHub Desktop.
Number to ordinal format
<?php
if (!function_exists('ordinal')) {
/**
* Number to ordinal format
*
* @param int $number
* @return string
*/
function ordinal($number)
{
$ends = ['st', 'nd', 'rd'];
$mod = $number % 100;
$key = ($number % 100) - 1;
if ($mod > 10 && $mod < 14) {
return $number . 'th';
}
return $number . (isset($ends[$key]) ? $ends[$key] : 'th');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment