# PHP 1st, 2nd, 3rd, 4th, 5th, 6th – PHP Add Ordinal Number Suffix - Comment Page: 1

This is simple function to add English ordinal number suffix after normal number. Function takes number as a parameter and returns number with suffix, like: 1st, 2nd, 3rd, 4th, 5th, 6th... [inttf_post_ad1] This is example table of ordinal numbers 1 to 100 1st 2nd 3rd 4th 5th 6th 7th 8th 9th ...

## 26 comments on “PHP 1st, 2nd, 3rd, 4th, 5th, 6th – PHP Add Ordinal Number Suffix - Comment Page: 1”

1 2

• Hi Lucy,

So could you tell more specific, what are you doing, if this is too complicated?

2. work perfectly! thanx!

3. lately i am seeing 1st , 2nd, 3rd, being written as 1th, 2th, 3th
Can anyone explain the concept behind this?

4. You guys are great! Keep it up! =))

5. Thanks. good explanation.

6. Thank you! You saved me some brain cells!

7. Thanks for this function..

8. Simple it is not, when it doesn’t provide a echo result!!!!

we have a \$num = 26

the result is =

exactly

I think karen had a POINT – a simple solution to a simple problem, one that works.

so what is the answer for 26 = 26st or or 26nd or 26rd or 26th

exactly karen’s point.

• Hi Clive,

Just tested and this function returns 26th as it should return…could you post your code?

If you try/run exactly following code:

``````
``````
``` What is your output?? Reply ```
``` ```
``` ```
• ``` ```
``` ```
9. ``` Clive Moore 23rd June 2013, 5:16 pm OK I am not joking either === Server error The website encountered an error while retrieving http://polishpotterymanufaktura.co.uk/assets/TimeLine_HISTORY/test26.php. It may be down for maintenance or configured incorrectly. Here is a LINK::: http://polishpotterymanufaktura.co.uk/assets/TimeLine_HISTORY/test26.php your code <?php function addOrdinalNumberSuffix(\$num) { if (!in_array((\$num % 100),array(11,12,13))){ switch (\$num % 10) { // Handle 1st, 2nd, 3rd case 1: return \$num.'st'; case 2: return \$num.'nd'; case 3: return \$num.'rd'; } } return \$num.'th'; } echo addOrdinalNumberSuffix(26), "\n"; ?> NOTE:: I added ?> also removed pre php at the head, but still SERVER ERROR! Clive Moore Reply JR 23rd June 2013, 10:35 pm What shows your access.log and error.log? Could you add example following code on same location to another file: Is it working? And if you have just mine code on this test26.php file, then you could simply login your server and run following command: ## Replace following path with your real path ## php /your/www/path/assets/TimeLine_HISTORY/test26.php What is output? Reply Jankoos 30th July 2013, 11:13 am Can you tell me how to use it in my case? how would I call the addOrdinalNumberSuffix and apply it to \$query_data[4]? column 4 is filled with days 1 to 31 and yes I know my php skills suck.. <?php mysql_connect("localhost", "user", "pass") or die (mysql_error ()); mysql_select_db("intranetbday") or die(mysql_error()); function addOrdinalNumberSuffix(\$num) { if (!in_array((\$num % 100),array(11,12,13))){ switch (\$num % 10) { // Handle 1st, 2nd, 3rd case 1: return \$num.'ste'; case 2: return \$num.'nd'; case 3: return \$num.'rd'; } } return \$num.'de'; } \$result = mysql_query("SELECT * FROM verjaarsdae WHERE WEEK( BD ) = WEEK( NOW() +1 ) LIMIT 0, 30 "); \$personeelWatVerjaar = mysql_num_rows(\$result); echo "”; echo “”,””,”Die week se verjaarsdae”,””,”” ; for(\$i=0;\$i<\$personeelWatVerjaar;\$i++) { \$query_data = mysql_fetch_row(\$result); echo "”,””, \$query_data[4], “”,””,”“,\$query_data[1],”“, “”,””,\$query_data[3], “”, “”; } echo “”; ?> Reply JR 31st July 2013, 1:57 pm Hi Jankoos, So just pass that \$query_data[4] to addOrdinalNumberSuffix function, like: echo "This is query_data[4] value, with ordinal number suffix: " . addOrdinalNumberSuffix(\$query_data[4]); Few other notes, mysql funtions are deprecated on PHP 5.5, so it’s better use PDO or at least mysqli instead mysql and more elegant way to do this loop is example following: \$result = mysql_query("SELECT field1, field2, field3 FROM verjaarsdae WHERE WEEK( BD ) = WEEK( NOW() +1 ) LIMIT 0, 30 "); while (\$row = mysql_fetch_assoc(\$result)) { echo \$row["field1"]; echo \$row["field2"]; echo \$row["field3"]; } Replace field1, field2, fiedl3…with your real field names or if you want really use numeric array, then use mysql_fetch_array instead mysql_fetch_assoc, but this is little bit risky, if you change your mysql table, then numeric array might be different. Reply sawyerrken 21st January 2014, 3:02 pm This function worked well for me. Thanks. Reply a 28th June 2014, 12:48 pm it will through some syntex error Reply JR 3rd July 2014, 10:52 am Hi a, Could you post errors? And PHP version? Reply ```
``` ```
``` 1 2 Next » Leave a Reply to Ryan Howe Cancel replyYour email address will not be published. Required fields are marked *Comment Name * Email * Website This site uses Akismet to reduce spam. Learn how your comment data is processed. ```
``` ```
``` ```
``` (adsbygoogle=window.adsbygoogle||[]).push({}); ```
``` Last updated posts Install Google Chrome on Fedora 32/31, CentOS/RHEL 8/7.7 Fedora 32/31/30 nVidia Drivers Install Guide Install Skype 8.56 on Fedora 31/30, CentOS/RHEL/SL 8.0/7.7 VirtualBox 6.1 on Fedora 31/30, CentOS/RHEL 8.0/7.7 Fedora 31 Netinstall / Desktop Install Guide Latest posts Fedora 31 Netinstall / Desktop Install Guide CentOS 8 Stream Install Guide – CentOS 8 Installation Screenshots Fedora 30 Netinstall / Server Install Guide Red Hat 8 Install Guide – RHEL 8 Installation Screenshots Fedora 29 Netinstall / Server Install Guide Most popular posts Fedora 32/31/30 nVidia Drivers Install Guide VirtualBox 6.1 on Fedora 31/30, CentOS/RHEL 8.0/7.7 Install Skype 8.56 on Fedora 31/30, CentOS/RHEL/SL 8.0/7.7 Fedora 16 nVidia Drivers Install Guide (disable nouveau driver) Install Google Chrome on Fedora 32/31, CentOS/RHEL 8/7.7 © If Not True Then False | Terms of Service / Disclaimer | Privacy Policy ```
``` Close function lCSS(src,onload=null){var s=document.createElement('link');s.href=src;s.rel='stylesheet';s.type='text/css';if(onload!=null){var a=document.createAttribute('onload');a.value=onload;s.setAttributeNode(a);}document.getElementsByTagName('head')[0].appendChild(s);}lCSS('https://fonts.googleapis.com/css?family=Lato:400,700',"document.documentElement.className+=' fontsloaded';");lCSS('/wp-content/themes/inttf/css/prism.css');lCSS('/wp-content/themes/inttf/css/baguetteBox.min.css'); document.querySelector('li.search a').addEventListener('click',function(){var o=document.getElementById('sov');var st=o.currentStyle?o.currentStyle.display:getComputedStyle(o,null).display;if(st==="none"){o.style.display="block";}else{o.style.display="none";}});document.querySelector('#sov .close').addEventListener('click',function(){document.getElementById('sov').style.display="none";}); window.addEventListener('load',function(){if(typeof baguetteBox!=='undefined'){baguetteBox.run('.entry-content');}}); ```