Jump to content


Photo

does anyone have an easy way to write the date superscript? ie "th" "rd"


  • Please log in to reply
4 replies to this topic

#1 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 03 October 2006 - 01:30 AM

i was thinking of writing a function that just takes the number of the date like 12 and ading the th by looping through arrays in a series of if elseif's

like

list($month, $day, $year) =  explode("-", date("m-d-Y", $time()));
$sp = "";

$rd = array(3,23);
$th = array(4,5,6,7,8,9,10,11,12,13,14,15,16 .....);

if(in_array($day, $rd)){
$sp = "rd";
}elseif etc.

any ideas that would shorten this logic?
thanks

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 October 2006 - 02:14 AM

Using the 'S' format character of the date function ...

English ordinal suffix for the day of the month, 2 characters


Legend has it that reading the manual never killed anyone.
My site

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 03 October 2006 - 02:15 AM

You can do this...

<?

$days = array ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 );

$days = array_map (create_function('$var', 'return $var . ( $var == 1 || $var == 21 || $var == 31 ? "st" : ( $var == 2 || $var == 22 ? "nd" : ( $var == 3 || $var == 23 ? "rd" : "th" ) ) );' ), $days );

print_r ( $days );

?>


me!

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 03 October 2006 - 05:05 AM

If you are using it for other than dates

http://www.phpfreaks....html#msg440793
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 03 October 2006 - 04:15 PM

thanks everyone. andy yo do not know how many times i looked at that page and was like "i guess they just dont have it"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users