Jump to content

Recommended Posts

Sooo to make a long story short, here's what im trying to accomplish.

 

I need to create a dropdown list of months, where the starting month is current month and the ending month is the previous months 2 years earlier.

 

Example of static version

<option VALUE="">-Month-</option>
<option VALUE="0" >Oct-2009</option>
<option VALUE="1" >Sep-2009</option>
<option VALUE="2" >Aug-2009</option>
<option VALUE="3" >Jul-2009</option>
<option VALUE="4" >Jun-2009</option>
<option VALUE="5" >May-2009</option>
<option VALUE="6" >Apr-2009</option>
<option VALUE="7" >Mar-2009</option>
<option VALUE="8" >Feb-2009</option>
<option VALUE="9" >Jan-2009</option>
<option VALUE="10" >Dec-2008</option>
<option VALUE="11" >Nov-2008</option>
<option VALUE="12" >Oct-2008</option>
<option VALUE="13" >Sep-2008</option>
<option VALUE="14" >Aug-2008</option>
<option VALUE="15" >Jul-2008</option>
<option VALUE="16" >Jun-2008</option>
<option VALUE="17" >May-2008</option>
<option VALUE="18" >Apr-2008</option>
<option VALUE="19" >Mar-2008</option>
<option VALUE="20" >Feb-2008</option>
<option VALUE="21" >Jan-2008</option>
<option VALUE="22" >Dec-2007</option>
<option VALUE="23" >Nov-2007</option>
<option VALUE="24" >Oct-2007</option>
<option VALUE="25" >Sep-2007</option>
<option VALUE="26" >Aug-2007</option>
<option VALUE="27" >Jul-2007</option>
<option VALUE="28" >Jun-2007</option>

 

Now it's easy to figure out current month through getDate();

The loop is relatively easy also. But i dont know how to use the date to subtract if you will the months and years from the current date.

 

So all in all i need to create the code above dynamically.

 

Thanks Guys!

Link to comment
https://forums.phpfreaks.com/topic/178232-solved-working-with-dates-help/
Share on other sites

I would use mktime() -

<?php
echo "<select>\n";
$now = getdate(); // array of current values "mon" and "year" 
for($i=0;$i<=28;$i++){
// hour, minute, second, month, day, year
$dt = mktime(0,0,0,$now['mon']-$i,1,$now['year']);
echo "<option VALUE='$i' >". date('M-Y',$dt) . "</option>\n";
}
echo "</select>";
?>

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.