dtyson2000 Posted February 5, 2013 Share Posted February 5, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/ Share on other sites More sharing options...
Christian F. Posted February 5, 2013 Share Posted February 5, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/#findComment-1410211 Share on other sites More sharing options...
dtyson2000 Posted February 6, 2013 Author Share Posted February 6, 2013 (edited) 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 February 6, 2013 by dtyson2000 Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/#findComment-1410413 Share on other sites More sharing options...
Barand Posted February 6, 2013 Share Posted February 6, 2013 $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> Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/#findComment-1410424 Share on other sites More sharing options...
dtyson2000 Posted February 7, 2013 Author Share Posted February 7, 2013 $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! Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/#findComment-1410704 Share on other sites More sharing options...
salathe Posted February 7, 2013 Share Posted February 7, 2013 … 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) Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/#findComment-1410822 Share on other sites More sharing options...
Barand Posted February 7, 2013 Share Posted February 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274052-php-date-strtotime-next-four-mondays-tuesdays-thursdays/#findComment-1410863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.