Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2006, 05:36 AM   PM User | #1
Taylor_1978
Regular Coder

 
Join Date: Jun 2003
Location: Australia
Posts: 528
Thanks: 8
Thanked 8 Times in 8 Posts
Taylor_1978 is on a distinguished road
1'st' 2'nd 3'rd' etc

I've searched PHP and it's more than likely a simple solution I just can't find. How do I turn a variable of 1 to 1st, 2 to 2nd etc...

Thanks in advance!
Taylor_1978 is offline   Reply With Quote
Old 11-22-2006, 05:52 AM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,711
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
You can use the date() function with the "S" format parameter for 1-31. Beyond that you would need to create your own function.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
CFMaBiSmAd is offline   Reply With Quote
Old 11-22-2006, 10:47 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
switch(day) {
1: 21: 31: day.='st'; break;
2: 22: day.='nd'; break;
3: 23: day.='rd'; break;
default: day.='th';
}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-23-2006, 06:24 PM   PM User | #4
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Both those assume it's a date, here's one that doesn't:
PHP Code:
function nth($in

if(
strlen($in)>&& substr($in,-2,1)==1
return 
$in."th"
switch(
substr($in,-1,1)) 

case 
1
return 
$in."st"
case 
2
return 
$in."nd"
case 
3
return 
$in."rd"
default: 
return 
$in."th"


__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:44 PM.


Advertisement
Log in to turn off these ads.