Jump to content

[SOLVED] english ordinal suffix


asmith

Recommended Posts

hey guys

how can i put english ordinal suffix beside a number ? 

in date function it is only putting S in the parathesis .

 

i've been searching a while , i couldn't find anyting, i'm sure i don't know the right keywords for it .

 

i have some numbers i want to print them with english ordinal suffix .

like i have  1, i want to echo 1st, (way too simple) ,  i have 2 , i want to echo 2nd  ...

 

thanks

Link to comment
Share on other sites

hey guys

how can i put english ordinal suffix beside a number ? 

in date function it is only putting S in the parathesis .

 

i've been searching a while , i couldn't find anyting, i'm sure i don't know the right keywords for it .

 

i have some numbers i want to print them with english ordinal suffix .

like i have  1, i want to echo 1st, (way too simple) ,  i have 2 , i want to echo 2nd  ...

 

thanks

Okay, coding this from post window. Hopefully will work.

<?
  	function ordinalSuffix($int, $sup = 0)
  {
	$a = substr($int, -1, 1);
	switch ($a)
		{
			case 1:
				$suff = "st";
				break;
			case 2:
				$suff = "nd";
				break;
			case 3:
				$suff = "rd";
				break;
			default:
				$suff = "th";
		}	
	if ($sup)
		return "$int<sup>$suff</sup>";
	else
		return $int . $suff;
}
$x = 1;
while ($x <= 100)
	{
		echo ordinalSuffix($x) . "<br />";
		$x++;
	}
?>

Works very well (tested).

Link to comment
Share on other sites

dear twostars,

 

surely i know to put a switch for every number !!!!

 

and i'm more sure php has a built-in function for this purpose.

Right, that's not what is happening.

Its checking the last numbers of the number.

 

You understand that if it ends in a 1, 2, or 3 it generally changes what the suffix will be (st, nd, rd).

However, forgot the check for 11, 12, and 13. That's below.

 

FIXED results and code are below. :P

 

1st
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
32nd
33rd
34th
35th
36th
37th
38th
39th
40th
41st
42nd
43rd
44th
45th
46th
47th
48th
49th
50th
51st
52nd
53rd
54th
55th
56th
57th
58th
59th
60th
61st
62nd
63rd
64th
65th
66th
67th
68th
69th
70th
71st
72nd
73rd
74th
75th
76th
77th
78th
79th
80th
81st
82nd
83rd
84th
85th
86th
87th
88th
89th
90th
91st
92nd
93rd
94th
95th
96th
97th
98th
99th
100th

 

<?
  	function ordinalSuffix($int, $sup = 0)
  {
	$a = substr($int, -1, 1);
	switch ($a)
		{
			case 1:
				if (substr($int, -2, 2) == 11)
					$suff = "th";
				else
					$suff = "st";
				break;
			case 2:
				if (substr($int, -2, 2) == 12)
					$suff = "th";
				else
					$suff = "nd";
				break;
			case 3:
				if (substr($int, -2, 2) == 13)
					$suff = "th";
				else
					$suff = "rd";
				break;
			default:
				$suff = "th";
		}	
	if ($sup)
		return "$int<sup>$suff</sup>";
	else
		return $int . $suff;
}
$x = 1;
while ($x <= 100)
	{
		echo ordinalSuffix($x) . "<br />";
		$x++;
	}
?>

Link to comment
Share on other sites

OK. So you changed it after I read it and while I did a forum search then posted the link.

 

BTW, It's only necessary to check if substr($num, -2, 1) == 1

 

<?php
function ordSuffix($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';
    }
}

Link to comment
Share on other sites

OK. So you changed it after I read it and while I did a forum search then posted the link.

 

BTW, It's only necessary to check if substr($num, -2, 1) == 1

 

<?php
function ordSuffix($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';
    }
}

That's true. Thanks for the tip. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.