Jump to content

Turning a number into a month and displaying it in a different format..


Recommended Posts

Hey,

What Im trying to do is take a number, make it a month (the numbers are from 1-12), and display it in the short text form of a month.

Variable $tmonmon is that number. So now how to transform it into a month and display it differently..

Any help appreciated.

Thanks
I have experience here from a reallllly crappy calendar system i made a couple years ago when i first started with php.

try somthing like
[code]switch($myVar) {

case 1 :
$day = "Jan";
break;

etc...

}[/code]
Hmmm...not sure if you understood my question. I think I wrote it wrong. Let me rephrase myself.
Actually, all I need to do, is be able to change the format of a month from double digit number to 3 letters
(ex. from 12 to Dec). The number exists as a variable.
Im trying:

substr("$tmon",2) = a number between 1 and 12.

$tmonmon = date("M", substr("$tmon",2));

However, when echoing this I always get the same month.

Ive tried isolating this and just hardcoding a value and it still doesnt work.

$tmonmon = date("M", 9);
echo("$tmonmon");

How come the above does not return Sep? It always returns Dec. Looks like it doesnt work they way I thought it did.

Thanks for you help!
The date() function returns the date on the server, so my guess is that it is returning the month that your server is set to..From my experience adding those numbers in there next to it aren't going to change it.  I believe there are however other date functions to add and subtract months, days, etc..
Yeah, unfortunately you might be right. I thought this would be a whole lot easier though. Simply modifying the date format. There is a date_format function, Im havin trouble using that one though. Ill keep on tryign with that one, and Ill let you all know how it goes...
The second parameter to the date() function is an integer that is the number of seconds since 1-1-1970. If you have a two digit number that represents a month, the easiest way to turn it into a symbolic month is to use an array:
[code]<?php
$months = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$tmonmon = $months[substr("$tmon",2)];
echo $tmonmon;
?>[/code]

You can also use a combinatio of strtotime() and date():
[code]<?php
$tmonmon = date('M',strtotime(substr("$tmon",2) . '/1/' . date('Y'));
echo $tmonmon;\
?>[/code]

Ken
[quote author=mkosmosports link=topic=121410.msg499160#msg499160 date=1168205715]
Hey,

What Im trying to do is take a number, make it a month (the numbers are from 1-12), and display it in the short text form of a month.

Variable $tmonmon is that number. So now how to transform it into a month and display it differently..

Any help appreciated.

Thanks
[/quote]$3LetterMonthName=switch($tmonmon){case 1:
return"Jan";
break;
case 2:
return"Feb";
break;
case 3:
return"Mar";
break;
case 4:
return"Apr";
break;
case 5:
return"May";
break;
case 6:
return"Jun";
break;
case 7:
return"Jul";
break;
case 8:
return"Aug";
break;
case 9:
return"Sep";
break;
case 10:
return"Oct";
break;
case 11:
return"Nov";
break;
case 12:
return"Dec";
break;}
This is the exact file I tested just now, it works fine;[code]<?php
$tmonmon=rand(1,12);
switch($tmonmon){case 1:
echo"Jan";
break;
case 2:
echo"Feb";
break;
case 3:
echo"Mar";
break;
case 4:
echo"Apr";
break;
case 5:
echo"May";
break;
case 6:
echo"Jun";
break;
case 7:
echo"Jul";
break;
case 8:
echo"Aug";
break;
case 9:
echo"Sep";
break;
case 10:
echo"Oct";
break;
case 11:
echo"Nov";
break;
case 12:
echo"Dec";
break;}
?>[/code]
Great!!

You guys have been mighty helpful. Thats one issue out of the way. Now, I gotta pick your brains' one more time with a date-related question. Im pulling dates from mysql with the following query:

SELECT DISTINCT DATE_FORMAT(START_DATE,'%y%c') FROM sf_team_player ORDER BY START_DATE DESC

This returns me all dates in a "yymm" type format. I then want to take the latest set from that date (hence the yymm date format), so:

while($row = mysql_fetch_array($alltransfermon)) (I need to extract all these dates for other purposes down the road)
{
$transfermon[] = $row["DATE_FORMAT(START_DATE,'%y%c')"];
}

$latesttr = max($transfermon);

echoing the above will give me that max in a yymm format. Now, my question is can php split out the mysql result $row["DATE_FORMAT(START_DATE,'%y%c')"] so I can get the max of the %y and then the max %c available for that %y. I want to then put those two variables in a url parameter....

Any ideas?

Thanks!!
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.