Ordinal Suffix

Add an ordinal suffix to a number, returning formats like 1st, 2nd, 3rd, 4th.

Code

General
$s = ['th', 'st', 'nd', 'rd'];
$v = $num % 100;
$suffix = $s[($v - 20) % 10] ?? $s[$v] ?? 'th';
return $num . $suffix;

Parameters

The number to add suffix to

Server

More PHP Snippets