Created
December 21, 2018 05:45
-
-
Save codemaker38/18d359380b52b3c173de47b484f14724 to your computer and use it in GitHub Desktop.
Number to ordinal format
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 | |
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