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! 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? 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 On 2/5/2013 at 1:00 PM, Christian F. said: 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? 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> 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 On 2/6/2013 at 12:25 PM, Barand said: $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! 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 On 2/6/2013 at 12:25 PM, Barand said: … 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) 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 On 2/7/2013 at 8:01 PM, salathe said: 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. 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
Archived
This topic is now archived and is closed to further replies.