mishasoni Posted July 28, 2009 Share Posted July 28, 2009 Quick question for a PHP guru: Is there a function for adding the "th", "rd", "nd", or "st" to a number? Eg. 21st, 33rd, 42nd...etc. Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/ Share on other sites More sharing options...
.josh Posted July 28, 2009 Share Posted July 28, 2009 function addCardinal($num = 0) { // I think that's what those things are called... $array = ((strlen($num) > 1) && (substr($num,-2,1) == '1'))? array('th','th','th','th','th','th','th','th','th','th') : array('th','st','nd','rd','th','th','th','th','th','th'); return $num . $array[substr($num,-1)]; } // end function addCardinal maybe shorter way to do it. First attempt. Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885418 Share on other sites More sharing options...
play_ Posted July 28, 2009 Share Posted July 28, 2009 This is slightly different than crayon's. If a number is just 0, it won't add 'th' to it. function ordinal($n) { switch( $n ) { case 1: $trail = 'st'; break; case 2: $trail = 'nd'; break; case 3: $trail = 'rd'; break; case 0: $trail = null; break; default: $trail = 'th'; } echo $n . $trail; }; Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885437 Share on other sites More sharing options...
ldougherty Posted July 28, 2009 Share Posted July 28, 2009 Found this script online... http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/ordinal_suffix.html Another good one here.. http://www.talkincode.com/create-ordinal-numbers-with-php-382.html function getOrdinal($number){ // get first digit $digit = abs($number) % 10; $ext = 'th'; $ext = ((abs($number) %100 < 21 && abs($number) %100 > 4) ? 'th' : (($digit < 4) ? ($digit < 3) ? ($digit < 2) ? ($digit < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th')); return $number.$ext; } Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885441 Share on other sites More sharing options...
.josh Posted July 28, 2009 Share Posted July 28, 2009 Okay I thought about it and I think I got everybody beat, even myself: date has a built-in way of determining the ordinal (I researched and found out the real name, though I see others already posted it!), so why not exploit that? function addOrdinal($num=0){return $num.(((strlen($num)>1)&&(substr($num,-2,1)=='1'))?'th':date("S",mktime(0,0,0,0,substr($num,-1),0)));} Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885445 Share on other sites More sharing options...
.josh Posted July 28, 2009 Share Posted July 28, 2009 This is slightly different than crayon's. If a number is just 0, it won't add 'th' to it. function ordinal($n) { switch( $n ) { case 1: $trail = 'st'; break; case 2: $trail = 'nd'; break; case 3: $trail = 'rd'; break; case 0: $trail = null; break; default: $trail = 'th'; } echo $n . $trail; }; play_ yours may not show the th on 0 but what yours doesn't consider is that only numbers ending in '11' use th so it's 21st, 22nd, 23rd..31st,32nd,33rd, etc.... Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885448 Share on other sites More sharing options...
play_ Posted July 29, 2009 Share Posted July 29, 2009 This is slightly different than crayon's. If a number is just 0, it won't add 'th' to it. function ordinal($n) { switch( $n ) { case 1: $trail = 'st'; break; case 2: $trail = 'nd'; break; case 3: $trail = 'rd'; break; case 0: $trail = null; break; default: $trail = 'th'; } echo $n . $trail; }; play_ yours may not show the th on 0 but what yours doesn't consider is that only numbers ending in '11' use th so it's 21st, 22nd, 23rd..31st,32nd,33rd, etc.... holy **** good catch Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885451 Share on other sites More sharing options...
Philip Posted July 29, 2009 Share Posted July 29, 2009 [edit i give up.] Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885471 Share on other sites More sharing options...
.josh Posted July 29, 2009 Share Posted July 29, 2009 Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885508 Share on other sites More sharing options...
Philip Posted July 29, 2009 Share Posted July 29, 2009 I 's Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-885555 Share on other sites More sharing options...
mishasoni Posted July 29, 2009 Author Share Posted July 29, 2009 It works! Thanks!! Link to comment https://forums.phpfreaks.com/topic/167871-function-for-adding-the-th-rd-nd-or-st-to-a-number/#findComment-886347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.