Skip to content

Instantly share code, notes, and snippets.

@kbanman
Created August 1, 2014 22:03
Show Gist options
  • Save kbanman/65706ca987043c046d9c to your computer and use it in GitHub Desktop.
Save kbanman/65706ca987043c046d9c to your computer and use it in GitHub Desktop.
Smarty ordinal modifier plugin
<?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