Jump to content

[SOLVED] Drop down list of months


sotusotusotu

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!)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.