Created
August 1, 2014 22:03
-
-
Save kbanman/65706ca987043c046d9c to your computer and use it in GitHub Desktop.
Smarty ordinal modifier plugin
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 | |
/** | |
* Gets the ordinal for a number | |
* | |
* @param integer $number | |
* @param string $format Format to render the number and ordinal | |
* @return string | |
*/ | |
function smarty_modifier_ordinal($number, $format = '%i%s') | |
{ | |
$number = intval($number); | |
$ordinals = [ | |
1 => 'st', | |
2 => 'nd', | |
3 => 'rd' | |
]; | |
$modulo = $number % 10; | |
if (array_key_exists($modulo, $ordinals)) { | |
$ordinal = $ordinals[$modulo]; | |
} else { | |
$ordinal = 'th'; | |
} | |
return sprintf($format, $number, $ordinal); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment