From d933c9eb04752496124ef4a5f5df6ffbaf0a1d87 Mon Sep 17 00:00:00 2001 From: George Petculescu Date: Thu, 20 Oct 2016 00:52:50 +0300 Subject: added ordinal_format() to Number helper; added to docs the info. --- system/helpers/number_helper.php | 29 +++++++++++++++++++++++++ user_guide_src/source/helpers/number_helper.rst | 17 ++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/system/helpers/number_helper.php b/system/helpers/number_helper.php index e7810c706..8e77c91ab 100644 --- a/system/helpers/number_helper.php +++ b/system/helpers/number_helper.php @@ -92,3 +92,32 @@ if ( ! function_exists('byte_format')) return number_format($num, $precision).' '.$unit; } } + +// ------------------------------------------------------------------------ + +if ( ! function_exists('ordinal_format')) +{ + /** + * Returns the English ordinal numeral for a given number + * + * @param int $number + * @return string + */ + function ordinal_format($number) + { + if ( ! is_int($number) OR $number < 1) + { + return FALSE; + } + + $ends = array('th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'); + if ((($number % 100) >= 11) && (($number % 100) <= 13)) + { + return $number.'th'; + } + else + { + return $number.$ends[$number % 10]; + } + } +} diff --git a/user_guide_src/source/helpers/number_helper.rst b/user_guide_src/source/helpers/number_helper.rst index 9d5e98cfb..20acdec18 100644 --- a/user_guide_src/source/helpers/number_helper.rst +++ b/user_guide_src/source/helpers/number_helper.rst @@ -49,4 +49,19 @@ The following functions are available: echo byte_format(45678, 2); // Returns 44.61 KB .. note:: The text generated by this function is found in the following - language file: *language//number_lang.php* \ No newline at end of file + language file: *language//number_lang.php* + +.. php:function:: ordinal_format($number) + + :param int $number: Non-negative natural number to be converted + :returns: Ordinal numeral for given number or FALSE on failure + :rtype: string + + Returns the ordinal numeral (1st, 2nd, 3rd etc.) for a + non-negative natural number. If the input is not a natural number + greater than 0, the function will return boolean FALSE. Examples:: + + echo ordinal_format(1); // Returns 1st + echo ordinal_format(3); // Returns 3rd + echo ordinal_format(21); // Returns 21st + echo ordinal_format(102); // Returns 102nd -- cgit v1.2.3-24-g4f1b