Jump to content

Function for adding the "th", "rd", "nd", or "st" to a number?


Recommended Posts

function  addCardinal($num = 0) { // I think that's what those things are called... 
  $array = ((strlen($num) > 1) && (substr($num,-2,1) == '1'))? 
           array('th','th','th','th','th','th','th','th','th','th')
           : array('th','st','nd','rd','th','th','th','th','th','th');
  return $num . $array[substr($num,-1)];
} // end function addCardinal

 

maybe shorter way to do it. First attempt.

This is slightly different than crayon's. If a number is just 0, it won't add 'th' to it.


function ordinal($n) {

switch( $n ) {
	case 1:
		$trail = 'st'; break;

	case 2:
		$trail = 'nd'; break;

	case 3:
		$trail = 'rd'; break;

	case 0:
		$trail = null; break;

	default:
		$trail = 'th';
}
echo $n . $trail;
};

 

 

 

Found this script online...

 

http://www.handyphp.com/index.php/PHP-Resources/Handy-PHP-Functions/ordinal_suffix.html

 

Another good one here..

 

http://www.talkincode.com/create-ordinal-numbers-with-php-382.html

 

function getOrdinal($number){
// get first digit
$digit = abs($number) % 10;
$ext = 'th';
$ext = ((abs($number) %100 < 21 && abs($number) %100 > 4) ? 'th' : (($digit < 4) ? ($digit < 3) ? ($digit < 2) ? ($digit < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $number.$ext;
}

Okay I thought about it and I think I got everybody beat, even myself: date has a built-in way of determining the ordinal (I researched and found out the real name, though I see others already posted it!), so why not exploit that?

 

function addOrdinal($num=0){return $num.(((strlen($num)>1)&&(substr($num,-2,1)=='1'))?'th':date("S",mktime(0,0,0,0,substr($num,-1),0)));}

 

:ninja:

This is slightly different than crayon's. If a number is just 0, it won't add 'th' to it.


function ordinal($n) {

switch( $n ) {
	case 1:
		$trail = 'st'; break;

	case 2:
		$trail = 'nd'; break;

	case 3:
		$trail = 'rd'; break;

	case 0:
		$trail = null; break;

	default:
		$trail = 'th';
}
echo $n . $trail;
};

 

 

 

 

play_ yours may not show the th on 0 but what yours doesn't consider is that only numbers ending in '11' use th so it's 21st, 22nd, 23rd..31st,32nd,33rd, etc....

This is slightly different than crayon's. If a number is just 0, it won't add 'th' to it.


function ordinal($n) {

switch( $n ) {
	case 1:
		$trail = 'st'; break;

	case 2:
		$trail = 'nd'; break;

	case 3:
		$trail = 'rd'; break;

	case 0:
		$trail = null; break;

	default:
		$trail = 'th';
}
echo $n . $trail;
};

 

 

 

 

play_ yours may not show the th on 0 but what yours doesn't consider is that only numbers ending in '11' use th so it's 21st, 22nd, 23rd..31st,32nd,33rd, etc....

 

 

holy **** good catch

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.