The only way I can think of doing this is have a if statement going up for every number you could possible have, IE, if(1) then "st" elseif (2) then "nd" etc etc if (23000) then "nd". It's a problem if you have big numbers but you could write a program to write the code for you, it could loop all the numbers printing the ifs for you to copy + paste into your code.
– Tom GullenJun 24 '10 at 12:52

2

@Tom, a lookup table might be better, just initialize it with the 23000 values and get the value at index n, where n is the number you want the ordinal.
– John BokerJun 24 '10 at 12:55

2

Col. Shrapnel. you may brilliant but not all. any way thanks for showing interest on my question
– ArKJun 24 '10 at 12:56

@John, very very clever idea, it would be very quick to access as well as each index represents the number you are looking up.
– Tom GullenJun 24 '10 at 12:56

Although a bit difficult to understand at first, I do now think it best represents how the ordinal suffix system works for English.
– eriscoJun 24 '10 at 14:44

4

I like your solution. Additionally if you prefer not to generate 0th modify the last line to be $abbreviation = ($number)? $number. $ends[$number % 10] : $number;
– Gavin JacksonFeb 26 '14 at 10:02

@GavinJackson your addition to this excellent solution really helped me out (+1). Could you please explain to me what is going on in the calculation? I want to understand. Cheers! EDIT: Found an answer: conditional operator
– Andrew FoxNov 11 '14 at 7:37

I like this approach, but alas, it doesn't work :-( 30th comes out as 30st. 40th comes out as 40st. etc.
– FlukeyJun 24 '10 at 13:59

1

Yeah, sorry. When I read the question I thought, hey this should be possible by a single line of code. And I just typed it away. As you see from my edits, I'm improving. After the third edit now I think it's quite done. At least all numbers from 1 to 150 print out nicely on my screen.
– PaulJun 24 '10 at 14:02

Looks good up to 500! (haven't tested it further than that). Good work! :-)
– FlukeyJun 24 '10 at 14:04

It does not check decimals, but will leave them in place (use floor($i), round($i), or ceil($i) at the beginning of the final return statement).

You could also add format_number($i) at the beginning of the final return statement to get a comma-separated integer (if you're displaying thousands, millions, etc.).

You could just remove the $i from the beginning of the return statement if you only want to return the ordinal suffix without what you input.

This function works commencing PHP 5.2 released November 2006 purely because of the short array syntax. If you have a version before this, then please upgrade because you're nearly a decade out of date! Failing that, just replace the in-line ['st', 'nd', 'rd'] with a temporary variable containing array('st', 'nd', 'rd');.

The same function (without returning the input), but an exploded view of my short function for better understanding:

Here's another very short version using the date functions. It works for any number (not constrained by days of the month) and takes into account that *11th *12th *13th does not follow the *1st *2nd *3rd format.

I am not quite sure what this answer offers in addition to ChintanThummar's answer. Well, it hints that ChintanThummar made a copyright violation, unless he wrote the code at your source...
– Andreas RejbrandFeb 4 '15 at 21:11

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).