Posted by : Jebastin Thursday 19 December 2013

It's a user-defined function which is a lot simpler than you think. Though there might be a .NET function already in existence for this, the following function (written in PHP) does the job. It shouldn't be too hard to port it over. This ordinal function will be useful when we needed to display the date format as 1st December 2013.
  1. function ordinal($num) {
  2.     $ones = $num % 10;
  3.     $tens = floor($num / 10) % 10;
  4.     if ($tens == 1) {
  5.         $suff = "th";
  6.     } else {
  7.         switch ($ones) {
  8.             case 1 : $suff = "st"; break;
  9.             case 2 : $suff = "nd"; break;
  10.             case 3 : $suff = "rd"; break;
  11.             default : $suff = "th";
  12.         }
  13.     }
  14.     return $num . $suff;
  15. }

 Related Function in other languages:

C# Code to Get Ordinal Suffix of a Number

Microsoft SQL Server Function to Get Ordinal Suffix of a Number

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -