{"id":485,"date":"2014-06-08T21:58:40","date_gmt":"2014-06-08T21:58:40","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=485"},"modified":"2014-06-08T22:20:29","modified_gmt":"2014-06-08T22:20:29","slug":"number-with-ordinal-suffix","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=485","title":{"rendered":"number with ordinal suffix"},"content":{"rendered":"<blockquote><p>Goal: to show the english ordinal number<\/p><\/blockquote>\n<p>I&#8217;ve found a very simle solution:<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nfunction get_ordinal_suffix($n) {\/\/ return English ordinal number\r\n  return $n.substr(date('jS', mktime(0,0,0,1,($n%10==0?9:($n%100&gt;20?$n%10:$n%100)),2000)),-2);\r\n};\r\n<\/pre>\n<p>a simple test and the result<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nfor($i=1; $i&lt;=31;$i++){\r\n\techo(ordinal_suffix($i).', ');\r\n} \r\nresult:\r\n1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st\r\n[\/php]\r\nor a second solution:\r\n[php]\r\nfunction ordinalSuffix( $n )\r\n{\r\n  return date('S',mktime(1,1,1,1,( (($n&gt;=10)+($n&gt;=20)+($n==0))*10 + $n%10) ));\r\n}\r\nresult:\r\nst, nd, rd, th, th, th, th, th, th, th, th, th, th, th, th, th, th, th, th, th, st, nd, rd, th, th, th, th, th, th, th, st\r\n<\/pre>\n<blockquote><p>source: <a href=\"http:\/\/stackoverflow.com\/questions\/3109978\/php-display-number-with-ordinal-suffix\">php-display-number-with-ordinal-suffix<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Goal: to show the english ordinal number I&#8217;ve found a very simle solution: a simple test and the result source: php-display-number-with-ordinal-suffix<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-485","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=485"}],"version-history":[{"count":6,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/485\/revisions"}],"predecessor-version":[{"id":491,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/485\/revisions\/491"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}