Mahngiel Posted May 6, 2012 Share Posted May 6, 2012 Hey Freaks, Anybody have a shortcut to implement ordinal numbers (1st, 2nd, 3rd) suffixes (sp? suffices??) ? My first thought was to throw a function at it: <?php function suffixizer( $number = 0 ) { if( $number == 0 ) return FALSE; elseif( substr($number, -1) == 1 ) return $suffixed = $number . 'st'; elseif( substr($number, -1) == 2 ) return $suffixed = $number . 'nd'; elseif( substr($number, -1) == 3 ) return $suffixed = $number . 'rd'; else return $suffixed = $number . 'th'; } ?> Any thoughts? [edit] forgot about 'th'[/edit] Quote Link to comment https://forums.phpfreaks.com/topic/262163-easy-ordinal-number-suffixes/ Share on other sites More sharing options...
Barand Posted May 6, 2012 Share Posted May 6, 2012 function ordSuffix($n) { $str = "$n"; $t = $n > 9 ? substr($str,-2,1) : 0; $u = substr($str,-1); if ($t==1) return $str . 'th'; else switch ($u) { case 1: return $str . 'st'; case 2: return $str . 'nd'; case 3: return $str . 'rd'; default: return $str . 'th'; } } echo ordSuffix(1).'<br>'; echo ordSuffix(11).'<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/262163-easy-ordinal-number-suffixes/#findComment-1343518 Share on other sites More sharing options...
Mahngiel Posted May 6, 2012 Author Share Posted May 6, 2012 I can definitely see the use of a switch working out well here. And good thinking on grabbing the last two digits... 1st != 11st Cheers, Barand. Any other input on this before i solve it? Quote Link to comment https://forums.phpfreaks.com/topic/262163-easy-ordinal-number-suffixes/#findComment-1343519 Share on other sites More sharing options...
Mahngiel Posted May 7, 2012 Author Share Posted May 7, 2012 Since Barand's function hasn't met any competitors, I'll mark this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/262163-easy-ordinal-number-suffixes/#findComment-1343590 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.