Jump to content

Recommended Posts

Hello...

 

I'm trying to calculate the next four Mondays, Tuesdays and Thursdays and stick each result in a select statement. So far, the best I have gotten is each date for Monday in four separate pulldowns:

 

for($i=1; $i<=4; $i++){
echo "<select><option>";
echo date("Y-m-d", strtotime('+'.$i.' Monday')).'<br>';
echo "</select>";
}

 

I guess the question is twofold. How do I get all of the dates in one pulldown and should I do three separate FOR statements to do it?

 

Thanks, in advance!

I think you're making this a bit more complicated than what it needs to be, by involving multiple loops.

Also, the reason you're getting the mondays in multiple drop-down menus, is because you've told PHP to repeat all of the HTML code. If you want only one drop-down then you'll need to move the select tags out of the loop. (Remove the br tag too and replace it with a closing option tag.)

As for adding the other days to the mix: Why not just copy the line that produces the mondays, and change it to read "tuesday" and "wednesday" instead? ;)

I think you're making this a bit more complicated than what it needs to be, by involving multiple loops.

Also, the reason you're getting the mondays in multiple drop-down menus, is because you've told PHP to repeat all of the HTML code. If you want only one drop-down then you'll need to move the select tags out of the loop. (Remove the br tag too and replace it with a closing option tag.)

As for adding the other days to the mix: Why not just copy the line that produces the mondays, and change it to read "tuesday" and "wednesday" instead? ;)

 

Thanks for the direction! I've done just what you said and all dates have been moved to the pulldown; however, the loop runs through each "day producing line" and the dates wind up out of order:

 


echo "<select>";
echo "<option>SELECT A DAY</option>";
for($i=1; $i<=4; $i++){
echo "<option>".date("Y-m-d", strtotime('+'.$i.' Monday'))."</option>";
echo "<option>".date("Y-m-d", strtotime('+'.$i.' Tuesday'))."</option>";
echo "<option>".date("Y-m-d", strtotime('+'.$i.' Thursday'))."</option>";
}
echo "</select>";

Produces:

2013-02-11
2013-02-12
2013-02-07
2013-02-18
2013-02-19
2013-02-14
2013-02-25
2013-02-26
2013-02-21
2013-03-04
2013-03-05
2013-02-28

 

Any thoughts or further direction on how to get the dates into order?

Edited by dtyson2000
$d = new DateTime();
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,2,4);
for ($i=0; $i<28; ++$i) {
   $d = $d->add($inc);
   if (in_array($d->format('w'), $required)) {
    $v = $d->format('Y-m-d');
    $t = $d->format('Y-m-d D');
    $dateOptions .= "<option value='$v'>$t</option>\n";
   }
}
?>
<select name="date">
   <option value=''>-select date-</option>
   <?php echo $dateOptions ?>
</select>

$d = new DateTime();
$inc = new DateInterval('P1D');
$dateOptions = '';
$required = array(1,2,4);
for ($i=0; $i<28; ++$i) {
$d = $d->add($inc);
if (in_array($d->format('w'), $required)) {
 $v = $d->format('Y-m-d');
 $t = $d->format('Y-m-d D');
 $dateOptions .= "<option value='$v'>$t</option>\n";
}
}
?>
<select name="date">
<option value=''>-select date-</option>
<?php echo $dateOptions ?>
</select>

 

Wow. This is exactly it. I didn't expect you to write the code entirely but thank you! I'll definitely look at it and learn from it! Thanks, again!

…
for ($i=0; $i<28; ++$i) {
    $d = $d->add($inc);
…

 

There's also the DatePeriod class whose purpose in life is to repeat an interval some number of times.

 

new DatePeriod($d, $inc, 28, DatePeriod::EXCLUDE_START_DATE)

There's also the DatePeriod class whose purpose in life is to repeat an interval some number of times.

 

Thanks for that, I hadn't come across that one - extremely powerful looking at examples in manual comments. Still learning.

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.