almightyegg Posted August 9, 2007 Share Posted August 9, 2007 How can I have a number variable and see whether it needs a st, nd, rd, or th at the end? like 1st, 2nd, 3rd, 4th... I originally thought I could select the last number but for example if it equaled 11, 12 or 13 the idea wouldn't work as they take th. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/ Share on other sites More sharing options...
MadTechie Posted August 9, 2007 Share Posted August 9, 2007 quick example from top of my head $day = 22; //example $NewDay = $day."th"; if($day == 1 || $day == 21 || $day == 31) { $NewDay = $day."st"; } if($day == 2 || $day == 22) { $NewDay = $day."nd"; } if($day == 3 || $day == 23) { $NewDay = $day."rd"; } Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319445 Share on other sites More sharing options...
almightyegg Posted August 9, 2007 Author Share Posted August 9, 2007 But some of my records will go into the thousands, and I can't exactly type out 1, 21, 31...1451 Maybe there is some adaptation to it that will while it out or something?? Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319454 Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 Not sure where I found this, I don't think its mine. <?php function ordinal($n) { $ln = (int) substr($n, -1); $sln = (int) substr($n, -2); $r = array('st','nd','rd'); $es = (($sln < 11 || $sln > 19) && $ln > 0 && $ln < 4); return $n . ($es ? $r[$ln - 1] : 'th'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319460 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 A majority of them are going to be "th", so the best way to do with is check for ones that won't be "th." $suffix = "th" $lastdigit = strlen($num)-1; if ($num[$lastdigit-1] != 1) { if ($num[$lastdigit] == 1) $suffix = "st"; else if ($num[$lastdigit] == 2) $suffix = "nd"; else if ($num[$lastdigit] == 3) $suffix = "rd"; } Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319464 Share on other sites More sharing options...
almightyegg Posted August 9, 2007 Author Share Posted August 9, 2007 @Thrope - That doesn't seem to have worked @lemmin - I got 1th lol Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319471 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 Oh, haha. That is because it doesn't check the size of the number, woops. $suffix = "th" $lastdigit = strlen($num)-1; if ($num[$lastdigit-1] != 1 || $lastdigit == 0) { if ($num[$lastdigit] == 1) $suffix = "st"; else if ($num[$lastdigit] == 2) $suffix = "nd"; else if ($num[$lastdigit] == 3) $suffix = "rd"; } That should fix it. Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319477 Share on other sites More sharing options...
almightyegg Posted August 9, 2007 Author Share Posted August 9, 2007 It still echoes 1th, and I changed it a little bit after seeing it hadn't worked and made it more readable for myself as I'm a bit of a n00b....still $lastdigit = strlen($num)-1; if ($num[$lastdigit-1] != 1 || $lastdigit == 0) { if($num[$lastdigit] == 1){ $suffix = "st"; }elseif($num[$lastdigit] == 2){ $suffix = "nd"; }elseif($num[$lastdigit] == 3){ $suffix = "rd"; }else{ $suffix = "th"; } } Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319483 Share on other sites More sharing options...
trq Posted August 9, 2007 Share Posted August 9, 2007 @Thrope - That doesn't seem to have worked Works fine here. <?php function ordinal($n) { $ln = (int) substr($n, -1); $sln = (int) substr($n, -2); $r = array('st','nd','rd'); $es = (($sln < 11 || $sln > 19) && $ln > 0 && $ln < 4); return $n . ($es ? $r[$ln - 1] : 'th'); } foreach(range(1,100) as $v) { echo ordinal($v) . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319485 Share on other sites More sharing options...
lemmin Posted August 9, 2007 Share Posted August 9, 2007 Woops, sorry, I am just not thinking. I was using the number as a string. Change it to this and it should work. $num = (string)$num; $suffix = "th"; $lastdigit = strlen($num)-1; if ($lastdigit == 0 || $num[$lastdigit-1] != 1) { if ($num[$lastdigit] == 1) $suffix = "st"; else if ($num[$lastdigit] == 2) $suffix = "nd"; else if ($num[$lastdigit] == 3) $suffix = "rd"; } I switched the comparisons around so that it wouldn't give an warning if $lastdigit was 0. Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319490 Share on other sites More sharing options...
almightyegg Posted August 9, 2007 Author Share Posted August 9, 2007 @lemmin - Ah that's great thanks @Thorpe - Oops, I had entered something wrong >.< sorry Quote Link to comment https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/#findComment-319496 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.