Pages

Showing posts with label ordinal numbers. Show all posts
Showing posts with label ordinal numbers. Show all posts

Monday, July 15, 2013

PHP code to add st, nd, rd or th to a number



function wp_get_ordinal($input_number)
{
  $number            = (string) $input_number;
  $last_digit        = substr($number, -1);
  $second_last_digit = substr($number, -2, 1);
  $suffix            = 'th';
  if ($second_last_digit != '1')
  {
    switch ($last_digit)
    {
      case '1':
        $suffix = 'st';
        break;
      case '2':
        $suffix = 'nd';
        break;
      case '3':
        $suffix = 'rd';
        break;
      default:
        break;
    }
  }
  if ((string) $number === '1') $suffix = 'st';
  return $number.$suffix;
}