Jump to content

convert 1,2,3,4 to 1st, 2nd, 3rd, 4th etc


delphi123

Recommended Posts

Sorry i just read the last part of what you said, so i wrote this.

 

Going to read the rest after i come back home...

so here...

<?php
for($x = 1; $x <= 10; $x++){

if($x == 1){
echo $x . "st<br>";
}
elseif($x == 2){

echo $x . "nd<br>";


}
elseif($x == 3){

echo $x . "rd<br>"; 


}
else
{

echo $x . "th<br>";

}
}

?>

 

With Mysql

 

<?php

$query = mysql_query("SELECT * FROM race ORDER BY place LIMIT 10");
$x = 1;

while($row = mysql_fetch_array($query)){

if($x == 1){
echo $x . "st" .$row['racer']. "<br>";
}
elseif($x == 2){

echo $x . "nd" .$row['racer']. "<br>";


}
elseif($x == 3){

echo $x . "rd" .$row['racer']. "<br>"; 


}
else
{

echo $x . "th " .$row['racer']. "<br>";

}




}
?>

Link to comment
Share on other sites

try

<?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';
    }
}

echo ordSuffix(1).'<br>';
echo ordSuffix(11).'<br>';

Link to comment
Share on other sites

Hi there,

 

I've been trying to use the following code, posted by Barand:

 

    					function ordSuffix($i) {
					   $str = "$i";
					   $t = $i > 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';
							   }
						}

                                       echo orderSuffix(1).'<br>';

 

But I get the following error:

Call to undefined function orderSuffix()

 

Does anyone know how I can fix this?

 

Many thanks,

 

Katie

 

Link to comment
Share on other sites

If the number is between 1 and 31 (inclusive) you can use the date() function in conjunction with the strtotime() function:

<?php
$num = rand(1,31); // get a number
echo date('jS',strtotime('2007-01-' . $num);
?>

 

Ken

 

What does this actually do?

 

It makes use of the date function's format to add the suffix onto the end of the date.  The format jS is the day of the month, followed by the relevant suffix. The second parameter is to set a particular date for this. It uses strtotime to create the unix timestamp based on a date, which is an arbitary year and month (though, of course, you need to use a month with 31 days!) followed by the day of the month, which is the number we want.

 

Quite a clever approach if you only need to go up to 31.

Link to comment
Share on other sites

If the number is between 1 and 31 (inclusive) you can use the date() function in conjunction with the strtotime() function:

<?php
$num = rand(1,31); // get a number
echo date('jS',strtotime('2007-01-' . $num);
?>

 

Ken

 

What does this actually do?

 

It makes use of the date function's format to add the suffix onto the end of the date.  The format jS is the day of the month, followed by the relevant suffix. The second parameter is to set a particular date for this. It uses strtotime to create the unix timestamp based on a date, which is an arbitary year and month (though, of course, you need to use a month with 31 days!) followed by the day of the month, which is the number we want.

 

Quite a clever approach if you only need to go up to 31.

 

Thats really neat.

 

*Saved Script*

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.