Skip to content

Instantly share code, notes, and snippets.

@Rudde
Created May 12, 2016 18:58
Show Gist options
  • Save Rudde/f88ab71ced795fdb5e6fe1062e3cfefc to your computer and use it in GitHub Desktop.
Save Rudde/f88ab71ced795fdb5e6fe1062e3cfefc to your computer and use it in GitHub Desktop.
Take a number input output it with proper suffix like 1st, 2nd, 3rd, and so on
<?php
/* Written by Barand from phpfreaks.com */
function numSuffix($n) {
$str = "$n";
$t = $n > 9 ? substr($str,-2,1) : 0;
$u = substr($str,-1);
if ($t==1) return $str . 'th';
else switch ($u) {
case 1: return $str . 'st';
case 2: return $str . 'nd';
case 3: return $str . 'rd';
default: return $str . 'th';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment