Page 1 of 1

generate month

Posted: Fri May 18, 2007 5:51 am
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

Posted: Fri May 18, 2007 6:01 am
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));
}

Posted: Fri May 18, 2007 6:13 am
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.

Posted: Fri May 18, 2007 6:19 am
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.

Posted: Fri May 18, 2007 6:27 am
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.

Posted: Fri May 18, 2007 6:28 am
by linkmania
Please explain the problem more specific

Posted: Fri May 18, 2007 6:32 am
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)

Posted: Fri May 18, 2007 6:42 am
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.

Posted: Sun May 20, 2007 12:45 am
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]

Posted: Sun May 20, 2007 7:02 am
by onion2k
My code doesn't do years. That wasn't in the original requirements.

Posted: Sun May 20, 2007 7:22 am
by feyd
strtotime() is your friend.

Posted: Sun May 20, 2007 9:38 am
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