Useful to create a title while iterating an array, or referencing the nth entity in some text.

1 -> 1st
2 -> 2nd
31 -> 31st

function nth($number) {
  if ($number % 10 == 1) {
    return $number.'st';
  } elseif ($number % 10 == 2) {
    return $number.'nd';
  } else {
    return $number.'th';
  }
}

Comments

sradomski’s picture

Status: Active » Needs review

Hehe - Forgot the 3rd case!

function nth($number) {
  if ($number % 10 == 1) {
    return $number.'st';
  } elseif ($number % 10 == 2) {
    return $number.'nd';
  } elseif ($number % 10 == 3) {
    return $number.'rd';
  } else {
    return $number.'th';
  }
}

d'oh

nancydru’s picture

Can you, please, supply this as a real patch - and in Drupal coding standards? Also, we require documentation to apply any new function.

nancydru’s picture

Status: Needs review » Closed (fixed)

Not submitted as a patch; no documentation.