Jump to content

Recommended Posts

Hello. I want to make this static listbox dynamic.

 

<select name="checkInMonthYear" id="checkInMonthYear" class="check">
<option value="May 2009">May 2009</option>
<option value="Jun 2009">Jun 2009</option>
<option value="Jul 2009">Jul 2009</option>
<option value="Aug 2009">Aug 2009</option>
<option value="Sep 2009">Sep 2009</option>
<option value="Oct 2009">Oct 2009</option>
<option value="Nov 2009">Nov 2009</option>
<option value="Dec 2009">Dec 2009</option>
<option value="Jan 2010">Jan 2010</option>
<option value="Feb 2010">Feb 2010</option>
<option value="Mar 2010">Mar 2010</option>
<option value="Apr 2010">Apr 2010</option>
</select>	

 

It needs to check the current month and year and then list the current "month/year" plus the next consecutive 11 "month/year" options.

 

Can someone point me in the right direction? Thanks.

 

 

PHP. A quick look in the manual would tell you that date('M') returns a three letter representation of the current month. And to get the next 11 months, you can use date('M', strtotime("+ $i months")) in a loop, where $i is increased by one on each iteration.

Sorry about that... Personally, I would just use PHP...

 

I constantly test what people post about on here and already figured this one out:

 

http://anomymage.com/tests/do/phpfreaks/

 

Might be the long way to do it, but it works:

 

<?php

$month = date("F o", mktime(0, 0, 0, date("m")));
$month1 = date("F o", mktime(0, 0, 0, date("m")+1));
$month2 = date("F o", mktime(0, 0, 0, date("m")+2));
$month3 = date("F o", mktime(0, 0, 0, date("m")+3));
$month4 = date("F o", mktime(0, 0, 0, date("m")+4));
$month5 = date("F o", mktime(0, 0, 0, date("m")+5));
$month6 = date("F o", mktime(0, 0, 0, date("m")+6));
$month7 = date("F o", mktime(0, 0, 0, date("m")+7));
$month8 = date("F o", mktime(0, 0, 0, date("m")+);
$month9 = date("F o", mktime(0, 0, 0, date("m")+9));
$month10 = date("F o", mktime(0, 0, 0, date("m")+10));
$month11 = date("F o", mktime(0, 0, 0, date("m")+11));

?>

<select name="checkInMonthYear" id="checkInMonthYear" class="check">
<option value="<?php echo $month; ?>"/><?php echo $month; ?></option>
<option value="<?php echo $month1; ?>"/><?php echo $month1; ?></option>
<option value="<?php echo $month2; ?>"/><?php echo $month2; ?></option>
<option value="<?php echo $month3; ?>"/><?php echo $month3; ?></option>
<option value="<?php echo $month4; ?>"/><?php echo $month4; ?></option>
<option value="<?php echo $month5; ?>"/><?php echo $month5; ?></option>
<option value="<?php echo $month6; ?>"/><?php echo $month6; ?></option>
<option value="<?php echo $month7; ?>"/><?php echo $month7; ?></option>
<option value="<?php echo $month8; ?>"/><?php echo $month8; ?></option>
<option value="<?php echo $month9; ?>"/><?php echo $month9; ?></option>
<option value="<?php echo $month10; ?>"/><?php echo $month10; ?></option>
<option value="<?php echo $month11; ?>"/><?php echo $month11; ?></option>
</select>

Might be the long way to do it

 

Yep! ;)

 

Loopy goodness:

 

<?php
echo '<select name="checkInMonthYear" id="checkInMonthYear" class="check">';
for ($i = 0; $i < 12; $i++) {
$date_str = date('M Y', strtotime("+ $i months"));
echo "\n<option value=\"$date_str\">$date_str</option>";
}
echo "\n</select>";
?>

Might be the long way to do it

 

Yep! ;)

 

Loopy goodness:

 

<?php
echo '<select name="checkInMonthYear" id="checkInMonthYear" class="check">';
for ($i = 0; $i < 12; $i++) {
$date_str = date('M Y', strtotime("+ $i months"));
echo "\n<option value=\"$date_str\">$date_str</option>";
}
echo "\n</select>";
?>

 

Fantastic... must update my snippets!

Hi jschmitt, I didn't mean to make my first post seem to ask for someone to do it for me and after your post I tried to reword it but the modify link was gone. I promise I am not a leach. :)

 

I'm studying your first post and it's... perfect. So just so I fully understand it and will be able to do this myself the next time (in other words, learn from you) I will explain it back.

 

$month = date("F o", mktime(0, 0, 0, date("m")));

 

You created a variable called month that is equal to the current date displayed as F (month) and o (year).

 

Within date() you used mktime to create a current unix timestamp and as your 4th argument (month) you used the current month.

 

Genius! 

Hi jschmitt, I didn't mean to make my first post seem to ask for someone to do it for me and after your post I tried to reword it but the modify link was gone. I promise I am not a leach. :)

 

I'm studying your first post and it's... perfect. So just so I fully understand it and will be able to do this myself the next time (in other words, learn from you) I will explain it back.

 

$month = date("F o", mktime(0, 0, 0, date("m")));

 

You created a variable called month that is equal to the current date displayed as F (month) and o (year).

 

Within date() you used mktime to create a current unix timestamp and as your 4th argument (month) you used the current month.

 

Genius!

 

I didn't think you were being a leech, I was just wondering what you had attempted really. I guess my first post came out wrong.

 

Thanks, but thebadbad actually made one better then mine. Mine will work just fine, but his is a simple loop...

 

But your re-iteration is definitely correct, and I appreciate the genius statement, but it is far from it.

wow, loopy goodness indeed. Thanks thebadbad and jsschmitt.

 

I'm understand this but it was at first admittedly beyond my current capabilities. You created a for loop that will run as long as i is less than 12. The variable $date_str is equal to the date Month and Year and you use the strtotime() function to add the value of "i" to months. Awesome.

 

<?php
echo '<select name="checkInMonthYear" id="checkInMonthYear" class="check">';
for ($i = 0; $i < 12; $i++) {
$date_str = date('M Y', strtotime("+ $i months"));
echo "\n<option value=\"$date_str\">$date_str</option>";
}
echo "\n</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.