Smarty ordinal suffix modifier plugin

 This Smarty modifier plugin adds ordinal suffix to a number, so 1 becomes 1st, 15 becomes 15th and so on.

Specially useful for dates formatting in english.

Usage:

{$myNumber|ordinal}

Installation:

Unzip and copy this file in your smarty/plugins folder.

Installation for ImpressCMS users:

Unzip and copy this file in libraries/smarty/icms_plugins folder.

Notes: This plugin uses Ordinal Suffix PHP function by Paul Ferrett (www.paulferrett.com/)

AttachmentSize
modifier.ordinal.zip635 bytes

Thank you! This plugin works brilliantly — saved quite a bit of time, too.

You're welcome!

(at last I have proofs someone is using this plugin).

I thought I'd offer a modification I made to the plugin since we have ordinal numbers > 999 and wanted them formated with commas, such as 27,987th.

I changed the function definition to this (basically just adding in the number_format options):

function smarty_modifier_ordinal($n, 
                                 $decimals = 0, 
                                 $dec_sep=",", 
                                 $thous_sep = ".")
{
  if (is_numeric($n)) {
    $n_last = $n % 100; 
    $n = number_format($n, $decimals, 
                       $dec_sep, $thous_sep);

    /* ...snipped... */

I then call it like this:

{$myNumber)|ordinal:'0':'.':','}

Thanks a lot for the suggestion, Jon.

Wow! have been trying to hack this functionality into my CMSMS installation for ages, now this little smarty plugin saves me so muhc time! thanks man!

I'm very happy it helped you. Thanks for visiting.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <i> <b> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <h2> <h3> <blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options