- Back to Home »
- PHP »
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.
- function ordinal($num) {
- $ones = $num % 10;
- $tens = floor($num / 10) % 10;
- if ($tens == 1) {
- $suff = "th";
- } else {
- switch ($ones) {
- case 1 : $suff = "st"; break;
- case 2 : $suff = "nd"; break;
- case 3 : $suff = "rd"; break;
- default : $suff = "th";
- }
- }
- return $num . $suff;
- }