generate month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bjen
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 4:42 am

generate month

Post by bjen »

Hi,

I want to created an drop down list that containing values like Jan, Feb, etc.
The drop down list should contains values for maximum 6 months.User able to select it.
For this the values have to be dynamically generated depending on the current month.

please help!

Thank you
Last edited by bjen on Fri May 18, 2007 6:01 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

$currentMonth = date("m");
$currentYear = date("Y");
for ($x=0;$x<6;$x++) {
  echo date("M",mktime(0,0,0,$currentMonth+$x,1,$currentYear));
}
bjen
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 4:42 am

Post by bjen »

Sorry onion2k, actually i not want to get the next current month value.....

Below is the correct question:

I want to created an drop down list that containing values like Jan, Feb, etc.
The drop down list should contains values for maximum 6 months.User able to select it.
For this the values have to be dynamically generated depending on the current month.

Please help up...

Thank you.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You've not stated what you actually want, just that you want a dynamic dropdown based on some mysterious conditions that you're not telling us. Without some indication what they'll be, or at least what the constraints for the conditions are, there's nothing much anyone can do.
bjen
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 4:42 am

Post by bjen »

For this drop down list will allow user to select max 6 months based on the calendar. This value have to generate depending on the current month.

Example:
Current month = May.
Drop down list will generating this values “May, Jun, July, Aug, Sep, Oct”

Thank you.
User avatar
linkmania
Forum Newbie
Posts: 4
Joined: Thu May 10, 2007 1:45 am
Location: India

Post by linkmania »

Please explain the problem more specific
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

http://www.timvw.be/generate-a-menu-with-month-names/...

This technique gives you the twelve month names (localizable).. All you need to do is figure out the current month index, and get the 6 following (simple while loop incrementing $index, and getting the element at position $index % 12)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If the months don't change...

Code: Select all

$currentMonth = date("m");
$currentYear = date("Y");
switch ($currentMonth) {
  case 1: $months = array(1,2,3,4,5,6); break;
  case 2: $months = array(2,3,4,5,6,7); break;
  case 3: $months = array(3,4,5,6,11,12); break;
  //and all the other months...
}
foreach ($months as $m) {
  echo date("M", mktime(0,0,0,$m,1,$currentYear));
}
If they do change, or if they're controlled via an admin page or something, then I'd store them in a database table.
bjen
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 4:42 am

Post by bjen »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi onion2k,

The year is not change once case 10, case 11 and case 12 is executed. 

Output Script:  Oct - 2007
	        Nov – 2007 
                        Dec – 2007
                        Jan – 2007
                        Feb – 2007
                        Mar – 2007

It supposes to be like below output:

[color=red]Oct - 2007
Nov – 2007  
Dec – 2007
Jan – 2008
Feb – 2008
Mar – 2008[/color]

Below is my script:

Code: Select all

switch ($month) 
{ 
   case 1: $months = array(1,2,3,4,5,6); break; 
  case 2: $months = array(2,3,4,5,6,7); break; 
  case 3: $months = array(3,4,5,6,7,8 ); break; 
  case 4: $months = array(4,5,6,7,8,9); break; 
  case 5: $months = array(5,6,7,8,9,10); break; 
  case 6: $months = array(6,7,8,9,10,11); break; 
  case 7: $months = array(7,8,9,10,11,12); break; 
  case 8: $months = array(8,9,10,11,12,1); break; 
  case 9: $months = array(9,10,11,12,1,2); break; 
  case 10: $months = array(10,11,12,1,2,3); break; 
  case 11: $months = array(11,12,1,2,3,4); break; 
  case 12: $months = array(12,1,2,3,4,5); break; 
} 


foreach ($months as $m) { 
  echo date("M - Y", mktime(0,0,0,$m,1,$currentYear)); 
}
Please Help!

Thank you


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

My code doesn't do years. That wasn't in the original requirements.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strtotime() is your friend.
bjen
Forum Newbie
Posts: 5
Joined: Fri May 18, 2007 4:42 am

Post by bjen »

Hi onion2k,

It is possible that I change the year within [s]yr’s[/s] your code? How can I do it? Please give some advise on it.

Your help will be very much appreciated.

Thank you
Post Reply