Jump to content

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


mishasoni

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.