mkosmosports Posted January 7, 2007 Share Posted January 7, 2007 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 Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/ Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155229 Share on other sites More sharing options...
mkosmosports Posted January 7, 2007 Author Share Posted January 7, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155270 Share on other sites More sharing options...
magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 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.. Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155283 Share on other sites More sharing options...
mkosmosports Posted January 7, 2007 Author Share Posted January 7, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155289 Share on other sites More sharing options...
kenrbnsn Posted January 8, 2007 Share Posted January 8, 2007 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 Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155376 Share on other sites More sharing options...
Azu Posted January 8, 2007 Share Posted January 8, 2007 [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;} Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155381 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Um... a switch returns nothing. It is a statement, not a function. Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155384 Share on other sites More sharing options...
Azu Posted January 8, 2007 Share Posted January 8, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155392 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 That may well, but thats not what you posted earlier. Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-155394 Share on other sites More sharing options...
mkosmosports Posted January 9, 2007 Author Share Posted January 9, 2007 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!! Quote Link to comment https://forums.phpfreaks.com/topic/33245-turning-a-number-into-a-month-and-displaying-it-in-a-different-format/#findComment-156303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.