Return to Snippet

Revision: 30834
at August 23, 2010 19:23 by flashmac


Initial Code
function make_position($position) {
	$last = substr( $position, -1 );
	$seclast = substr( $position, -2, -1 );
	if( $last > 3 || $last == 0 ) $ext = 'th';
	else if( $last == 3 ) $ext = 'rd';
	else if( $last == 2 ) $ext = 'nd';
	else $ext = 'st'; 

	if( $last == 1 && $seclast == 1) $ext = 'th';
	if( $last == 2 && $seclast == 1) $ext = 'th';
	if( $last == 3 && $seclast == 1) $ext = 'th'; 

	return $position.$ext;
}

eg. make_position(2); // 2nd
eg. make_position(8); // 8th
eg. make_position(21); // 21st
eg. make_position(23); // 23rd

Initial URL
www.stevenmcintosh.co.uk

Initial Description
If you want to transform an int number to a position number (ie. 1 = 1st, 6 = 6th etc..)

Initial Title
Make Position Number

Initial Tags


Initial Language
PHP