asmith Posted January 12, 2008 Share Posted January 12, 2008 hey guys how can i put english ordinal suffix beside a number ? in date function it is only putting S in the parathesis . i've been searching a while , i couldn't find anyting, i'm sure i don't know the right keywords for it . i have some numbers i want to print them with english ordinal suffix . like i have 1, i want to echo 1st, (way too simple) , i have 2 , i want to echo 2nd ... thanks Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 hey guys how can i put english ordinal suffix beside a number ? in date function it is only putting S in the parathesis . i've been searching a while , i couldn't find anyting, i'm sure i don't know the right keywords for it . i have some numbers i want to print them with english ordinal suffix . like i have 1, i want to echo 1st, (way too simple) , i have 2 , i want to echo 2nd ... thanks Okay, coding this from post window. Hopefully will work. <? function ordinalSuffix($int, $sup = 0) { $a = substr($int, -1, 1); switch ($a) { case 1: $suff = "st"; break; case 2: $suff = "nd"; break; case 3: $suff = "rd"; break; default: $suff = "th"; } if ($sup) return "$int<sup>$suff</sup>"; else return $int . $suff; } $x = 1; while ($x <= 100) { echo ordinalSuffix($x) . "<br />"; $x++; } ?> Works very well (tested). Quote Link to comment Share on other sites More sharing options...
asmith Posted January 12, 2008 Author Share Posted January 12, 2008 dear twostars, surely i know to put a switch for every number !!!! and i'm more sure php has a built-in function for this purpose. thanks for taking time anyway. Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 dear twostars, surely i know to put a switch for every number !!!! and i'm more sure php has a built-in function for this purpose. Right, that's not what is happening. Its checking the last numbers of the number. You understand that if it ends in a 1, 2, or 3 it generally changes what the suffix will be (st, nd, rd). However, forgot the check for 11, 12, and 13. That's below. FIXED results and code are below. 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st 32nd 33rd 34th 35th 36th 37th 38th 39th 40th 41st 42nd 43rd 44th 45th 46th 47th 48th 49th 50th 51st 52nd 53rd 54th 55th 56th 57th 58th 59th 60th 61st 62nd 63rd 64th 65th 66th 67th 68th 69th 70th 71st 72nd 73rd 74th 75th 76th 77th 78th 79th 80th 81st 82nd 83rd 84th 85th 86th 87th 88th 89th 90th 91st 92nd 93rd 94th 95th 96th 97th 98th 99th 100th <? function ordinalSuffix($int, $sup = 0) { $a = substr($int, -1, 1); switch ($a) { case 1: if (substr($int, -2, 2) == 11) $suff = "th"; else $suff = "st"; break; case 2: if (substr($int, -2, 2) == 12) $suff = "th"; else $suff = "nd"; break; case 3: if (substr($int, -2, 2) == 13) $suff = "th"; else $suff = "rd"; break; default: $suff = "th"; } if ($sup) return "$int<sup>$suff</sup>"; else return $int . $suff; } $x = 1; while ($x <= 100) { echo ordinalSuffix($x) . "<br />"; $x++; } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted January 12, 2008 Share Posted January 12, 2008 Umm, 11st, 12nd, 13rd ??? ??? ??? See http://www.phpfreaks.com/forums/index.php/topic,169178.msg746447.html#msg746447 if you don't want numbers > 31 you can use date() - see post above the one I linked to Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 Umm, 11st, 12nd, 13rd ??? ??? ??? See http://www.phpfreaks.com/forums/index.php/topic,169178.msg746447.html#msg746447 if you don't want numbers > 31 you can use date() - see post above the one I linked to Re-read my post. I already fixed it. Spotted that almost immediately. Quote Link to comment Share on other sites More sharing options...
Barand Posted January 12, 2008 Share Posted January 12, 2008 OK. So you changed it after I read it and while I did a forum search then posted the link. BTW, It's only necessary to check if substr($num, -2, 1) == 1 <?php 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'; } } Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 OK. So you changed it after I read it and while I did a forum search then posted the link. BTW, It's only necessary to check if substr($num, -2, 1) == 1 <?php 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'; } } That's true. Thanks for the tip. Quote Link to comment 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.