Ad Code

Display numbers with ordinal suffix in PHP

Method - 1

function ordinal($numm) {
   $num = trim($numm);
   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';
}

Method - 2

$locale = 'en_US';
$nf = new NumberFormatter($locale, NumberFormatter::ORDINAL);
echo $nf->format($number);

 


 

Post a Comment

0 Comments