Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created February 20, 2011 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/836280 to your computer and use it in GitHub Desktop.
Save joshhartman/836280 to your computer and use it in GitHub Desktop.
Convert Number to Ordinal Number with Suffix Text
<?php
date_default_timezone_set('America/Chicago');
// This function will take a number and add "th, st, nd, rd, th" after it. For example: echo ordinal(10); // outputs '10th'
function ordinal($i){
$l=substr($i,-1);$s=substr($i,-2,-1);return$i.(($l==1&&$s==1)||($l==2&&$s==1)||($l==3&&$s==1)||$l>3||$l==0?'th':($l==3?'rd':($l==2?'nd':'st')));
}
echo 'Today is the '.ordinal(date('z')).' day of this '.ordinal(date('Y')).' year of the common era.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment