Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/64102-solved-number-st-nd-rd-th/
Share on other sites

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";
}

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');
  }

?>

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";
}

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.

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";
}
}

@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 />";
  }

?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.