Jump to content

Date exclusion on form population


Anspaujd
Go to solution Solved by jcbones,

Recommended Posts

I am working on a form with php generated values. What I have right now is as follows:

<tr>
<td><p>ENTRY TERM: <select name="entry_term" >
<option value="">Choose One:</option>
<?php 
$year=date("Y");
for ($y=0;$y<5;$y++) {
	echo '<option value="Fall '.($year+$y).'">Fall '.($year+$y).'</option>';
	echo '<option value="Spring '.($year+$y+1).'">Spring '.($year+$y+1).'</option>';
}
?>
</select>
</p></td>

This currently generates a list of :

 

Fall 2014

Spring 2015

Fall 2015

Spring 2016

Fall 2016

...etc for next 5 years

 

What I need to do is have this time sensitive to exclude once a specific month has passed. For example once August 2014 has started "Fall 2014" should be excluded... or when Jan 2015 has started "Spring 2015" should be excluded.

 

Can someone help me solve this as I am unsure of how to move forward with this.

 

Thanks,

Link to comment
Share on other sites

 

for ($y=0;$y<5;$y++) 
{
    if ($y==0)
        if (Date('m') <7)
           echo '<option value="Fall '.($year+$y).'">Fall '.($year+$y).'</option>';
    echo '<option value="Spring '.($year+$y+1).'">Spring '.($year+$y+1).'</option>';
}

This seems like it would eliminate the first occurrence of Fall once July has passed.  We need more info on your Spring condition though.  You do realized that once January has begun your initial $year value will not be 2014 again, so basically all dates will be > Jan 2015

  • Like 1
Link to comment
Share on other sites

Yes I understand that the code as is will cut based on July which is why I am seeking guidance on expanding it to cut the current Fall if August is passed or cut the current Spring if January is passed.

 

I have tried IF / ELSE but I can only make this work for the current month rather than excluding based on a specific month.

 

Ultimately, I need this to read as follows:

 

Fall 2014

Spring 2015

Fall 2015

... etc

 

if the month is before August

 

After August it should read:

 

Spring 2015

Fall 2015

Spring 2016

... etc

 

AT THE SAME TIME THE FORM NEEDS TO ALSO READ FOR JANUARY... meaning if January is passed it should read as:

 

Fall 2015

Spring 2016

Fall 2016

... etc

 

Does this make sense?

 

I was able to make it work to cut the Fall based on July as the month (or any month) but I need it to also dictate excluding Spring if the month is January.

Link to comment
Share on other sites

  • Solution

Try this:

 

<?php 
$year=date("Y");
for ($y=0;$y<5;$y++) {
$month = date('n');
$controlYear = date('Y');
if(($month <= 1 && $controlYear == $year) || $controlYear != $year){
echo '<option value="Spring '.($year).'">Spring '.($year).'</option>';
}
if(($month <= 7 && $controlYear == $year) || $controlYear != $year) {
echo '<option value="Fall '.($year).'">Fall '.($year).'</option>';
}
++$year;
}
?>
 
Link to comment
Share on other sites

You originally you wanted to skip Fall 2014 once August started - not finished.  Plus once Jan 2015 arrives you will never encounter Fall 2014.  So  my example does exactly what you asked for for the Fall date, but you haven't said how you want to handle Jan 2015.  Describe the time period - exactly - that you want to use to exclude Spring.  As your description reads now, one will never see Spring for the current year.  Is that what you want?  If so you don't have to do anything to exclude it since the first one that can ever show up is the Spring for the following year which is not a problem according to you.  So my post should do what you want.

Link to comment
Share on other sites

Start 6 months from current date then every 6 months. Spring if in first half of the year, Fall otherwise

$today = '2014-07-01';      // used to set todays date for testing

$dt1 = new DateTime($today);
$dt1->add(new DateInterval('P6M'));
$dt2 = clone $dt1;
$dt2->add(new DateInterval('P5Y'));
$dp = new DatePeriod($dt1, new DateInterval('P6M'), $dt2);       // dates every 6 months for 5 years

foreach ($dp as $d) {
    echo $d->format("Y ");
    if (1 <= $d->format('n') && $d->format('n') <= 6) {
        echo "Spring<br>";
    }
    else {
        echo "Fall<br>";
    }
}
  • Like 3
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.