Jump to content

Date loop


dc_jt

Recommended Posts

Hi I am constantly using drop down menus for dates for various projects I work on.

 

At the moment Im using a very poor strategy, as follows:

 

	$form_sdate = (date("d/m/Y"));
    $hygine3=substr($form_sdate, 0, 2);
$form_sdated= ($hygine3);
$hygine2=substr($form_sdate, 3,2);
$form_sdatem= ($hygine2);
$hygine1=substr($form_sdate, 6, ;
$form_sdatey= ($hygine1);

 

<td width="40%" COLSPAN=2>Approval Date


                              <select name="sdated" class="fmgreynormal">
                              
                                  <option value="01" <? if ($form_sdated == '1'){ echo "selected='selected'";}?>>01</option>
                                  <option value="02" <? if ($form_sdated == '2'){ echo "selected='selected'";}?>>02</option>
                                  <option value="03" <? if ($form_sdated == '3'){ echo "selected='selected'";}?>>03</option>
                                  <option value="04" <? if ($form_sdated == '4'){ echo "selected='selected'";}?>>04</option>
                                  <option value="05" <? if ($form_sdated == '5'){ echo "selected='selected'";}?>>05</option>
                                  <option value="06" <? if ($form_sdated == '6'){ echo "selected='selected'";}?>>06</option>
                                  <option value="07" <? if ($form_sdated == '7'){ echo "selected='selected'";}?>>07</option>
                                  <option value="08" <? if ($form_sdated == '8'){ echo "selected='selected'";}?>>08</option>
                                  <option value="09" <? if ($form_sdated == '9'){ echo "selected='selected'";}?>>09</option>
                                  <option value="10" <? if ($form_sdated == '10'){ echo "selected='selected'";}?>>10</option>
                                  <option value="11" <? if ($form_sdated == '11'){ echo "selected='selected'";}?>>11</option>
                                  <option value="12" <? if ($form_sdated == '12'){ echo "selected='selected'";}?>>12</option>
                                  <option value="13" <? if ($form_sdated == '13'){ echo "selected='selected'";}?>>13</option>
                                  <option value="14" <? if ($form_sdated == '14'){ echo "selected='selected'";}?>>14</option>
                                  <option value="15" <? if ($form_sdated == '15'){ echo "selected='selected'";}?>>15</option>
                                  <option value="16" <? if ($form_sdated == '16'){ echo "selected='selected'";}?>>16</option>
                                  <option value="17" <? if ($form_sdated == '17'){ echo "selected='selected'";}?>>17</option>
                                  <option value="18" <? if ($form_sdated == '18'){ echo "selected='selected'";}?>>18</option>
                                  <option value="19" <? if ($form_sdated == '19'){ echo "selected='selected'";}?>>19</option>
                                  <option value="20" <? if ($form_sdated == '20'){ echo "selected='selected'";}?>>20</option>
                                  <option value="21" <? if ($form_sdated == '21'){ echo "selected='selected'";}?>>21</option>
                                  <option value="22" <? if ($form_sdated == '22'){ echo "selected='selected'";}?>>22</option>
                                  <option value="23" <? if ($form_sdated == '23'){ echo "selected='selected'";}?>>23</option>
                                  <option value="24" <? if ($form_sdated == '24'){ echo "selected='selected'";}?>>24</option>
                                  <option value="25" <? if ($form_sdated == '25'){ echo "selected='selected'";}?>>25</option>
                                  <option value="26" <? if ($form_sdated == '26'){ echo "selected='selected'";}?>>26</option>
                                  <option value="27" <? if ($form_sdated == '27'){ echo "selected='selected'";}?>>27</option>
                                  <option value="28" <? if ($form_sdated == '28'){ echo "selected='selected'";}?>>28</option>
                                  <option value="29" <? if ($form_sdated == '29'){ echo "selected='selected'";}?>>29</option>
                                  <option value="30" <? if ($form_sdated == '30'){ echo "selected='selected'";}?>>30</option>
                                  <option value="31" <? if ($form_sdated == '31'){ echo "selected='selected'";}?>>31</option>
                                </select>
                              		
                              	
                                  <select name="sdatem" class="fmgreynormal">
                                    <option value="01" <? if ($form_sdatem == '01'){ echo "selected='selected'";}?>>January</option>
                                    <option value="02" <? if ($form_sdatem == '02'){ echo "selected='selected'";}?>>Febuary</option>
                                    <option value="03" <? if ($form_sdatem == '03'){ echo "selected='selected'";}?>>March</option>
                                    <option value="04" <? if ($form_sdatem == '04'){ echo "selected='selected'";}?>>April</option>
                                    <option value="05" <? if ($form_sdatem == '05'){ echo "selected='selected'";}?>>May</option>
                                    <option value="06" <? if ($form_sdatem == '06'){ echo "selected='selected'";}?>>June</option>
                                    <option value="07" <? if ($form_sdatem == '07'){ echo "selected='selected'";}?>>July</option>
                                    <option value="08" <? if ($form_sdatem == '08'){ echo "selected='selected'";}?>>August</option>
                                    <option value="09" <? if ($form_sdatem == '09'){ echo "selected='selected'";}?>>September</option>
                                    <option value="10" <? if ($form_sdatem == '10'){ echo "selected='selected'";}?>>October</option>
                                    <option value="11" <? if ($form_sdatem == '11'){ echo "selected='selected'";}?>>November</option>
                                    <option value="12" <? if ($form_sdatem == '12'){ echo "selected='selected'";}?>>December</option>
                                  </select>
                                  
                                  <select name="sdatey" class="fmgreynormal">
                                    <option value="2007" <? if ($form_sdatey == '2007'){ echo "selected='selected'";}?>>2007</option>
                                    <option value="2008" <? if ($form_sdatey == '2008'){ echo "selected='selected'";}?>>2008</option>
                                    <option value="2009" <? if ($form_sdatey == '2009'){ echo "selected='selected'";}?>>2009</option>
                                    <option value="2010" <? if ($form_sdatey == '2010'){ echo "selected='selected'";}?>>2010</option>
                                    <option value="2011" <? if ($form_sdatey == '2011'){ echo "selected='selected'";}?>>2011</option>
                                    <option value="2012" <? if ($form_sdatey == '2012'){ echo "selected='selected'";}?>>2012</option>
                                  </select>

 

I know there is a way of doing it using a loop but im not sure how. Does anyone have any templates for this as it will always be the same everytime its used, i just want todays date.

 

The field I am saving it in is a datetime field called approval_date.

 

Thanks for your help

Link to comment
Share on other sites

Is this what you are looking for?

 

 

<?php
$today = getdate();
$day = $today['mday'];
// I have my personal ones default to yesterday, so
$day -= 1;
$month = $today['mon'];
$mtharr = array("January","February","March","April","May","June","July","August","September","October","November","December");
?>

<table cellpadding="3" cellspacing="2" border="0"> 
<tr> 
<td valign="top"> 
<select name="sday"> 
<? 
for ($i=1;$i<=31;$i++) {?><? 
  echo "<option value=\"$i\""; 
  if ($i == $day) echo " selected"; 
  echo ">$i</option>"; 
} 
?> 
</select></td> 
<td valign="top"> 
<select name="smonth"> 
<? 
for ($i=1;$i<=12;$i++) {?><? 
  echo "<option value=\"$i\""; 
  if ($i == $month) echo " selected"; 
  echo ">" . $mtharr[$i-1] . "</option>"; 
} 
?> 
</select></td> 
<td valign="top"> 
<select name="syear"> 
  <option value="2004">2004</option> 
  <option value="2003">2003</option> 
</select></td> 
</tr> 
</table>

 

Link to comment
Share on other sites

you can change the last bit to give you more than just 2 years on the year select also...

 

<select name="syear"> 
<?php
  for ($i=1970;$i<=2010;$i++)
    {?><?php echo "<option value=\"$i\"";
    if ($i == $year) echo " selected";
    echo ">" .$i. "</option>";
    }
?>
</select></td> 

Link to comment
Share on other sites

Thanks a lot, thats exactly what I was looking for.

 

However, in the year section, is it possible to not have a limit on the year.

 

For example, on the one above the limit is 2010 but when it gets to 2011 this will be no good.

 

Could you set it to todays year+3 or something so every year it will be updated??

 

Thanks again for your help

Link to comment
Share on other sites

in the last post I made...

 

in the line

 

for ($i=1970;$i<=2010;$i++)

 

you can change those numbers to the limits of the years you want available.

you could even make them dynamic by assigning variables pulling those from current date +3 years or something like that.

Link to comment
Share on other sites

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.