Jump to content

Recommended Posts

Hello,

 

I am trying to create a drop down list with 12 months as the contents. I need to be able to determine what the latest month is and then put the 11 months before into it (in the correct order). For example, it is September 2007 obviously at the moment, so that will need to be the last and latest month in the list. The other 11 will be in reverse order going back to August 2006.

 

I am a newbie at PHP so could give me any ideas please?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/70777-solved-drop-down-list-of-months/
Share on other sites

Thanks a lot mate.  That displays exactly how I want it to. 

 

Just one more thing I need the list to link to pages. For example, September 2007 needs to link to "?month=sep2007" all the way down to "?month=oct2007".

 

Can this be achieved with that code?

 

 

Well, if you changed the value of the option to:

 

<?php
echo '<select name="month">';
for($x=0;$x<12;$x++){
$time = strtotime("$x months ago");
$month = date('F Y',$time);
$link = date('MY',$time);
echo '<option value="'.$link.'">'.$month.'</month>';
}
echo '</select>';
?>

 

And submitted the form with GET then yes, it would work.

Thanks guys. I tried it in a GET form but the month doesn't seem to change in the url. The month is always sept2007.

 

<form method="get" action="">
	<p>
			<?php
echo '<select name="month">';
for($x=0;$x<12;$x++){
$time = strtotime("$x months ago");
$month = date('F Y',$time);
$link = date('MY');
echo '<option value="'.$link.'">'.$month.'</month>';
}
echo '</select>';
?>
</p>
	<p>
			<label>
			<input type="submit" value="Submit" />
			</label>
</p>
</form>

 

Any ideas?

You made a little mistake when copying my post. Change this line:

$link = date('MY');

to:

$link = date('MY',$time);

 

Without the second parameter, the date function will return the formatted string for the current date, hence the reason why the value was always sep2007 (well, it would be till we get to october anyway!)

 

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.