Hi guys, I've created a booking form on my website for people to book appointments from a list of dates. I have it set to produce 120 days from the current day. It's been working fine all year but since the 120th day has hit December 31st/Jan 1st it's no longer producing the list. I can only assume this is due to it trying to look further than December in the same year - which there obviously isn't any more days in this year... So, how do I get it to start a new year, from Jan 1st..? Any help would be great. <option selected disabled value="select-date">Select a date</option> <?php date_default_timezone_set('Europe/London'); // Start date $date = date('l jS F'); // End date $end_date = date('l jS F', strtotime("+120 days")); while (strtotime($date) <= strtotime($end_date)) { echo "<option value=\"$date\n\">$date\n</option>"; $date = date ('l jS F', strtotime("+1 days", strtotime($date))); } ?> </select> PHP:
This is not working for another year, because your parameter for the date function i.e. $date=date('l jS F'); doesn't include information for year. So the loop doesn't go beyond current year. use this script. date_default_timezone_set('Europe/London'); for($i=0; $i<=120; $i++) { $time = time() + 24 * 60 * 60 * $i; $date = date ('l jS F',$time); echo "<option value=\"$date\n\">$date\n</option>"; } PHP:
samyak has it right, with no year in your repeating date, you'll not get proper behavior. Also if the value inside a OPTION tag is the same as it's value, you don't have to say value... and as such there's no real reason to use quite so many vars or quite so complex a math inside it.... much less calling time() every single loop which is far slower than storing the value ahead of time. (much less you could have bugs due to midnight rollover mid-execution). I'd simplify that down to: date_default_timezone_set('Europe/London'); $time = time(); $end = $time + 10368000; do { echo '<option>', date('l jS F', $time), '</option>'; } while (($time += 84600) <= $end); Code (markup): For reference: 84,600 is the number of seconds in a day (60*60*24) while 10,368,000 is the number of seconds in 120 days (84,600 * 120). Do the math of fixed values ahead of time so the parser doesn't have to. *** SIDE NOTE *** I hope that 120 count is for testing and not real-world use -- anything more than ~16 or so OPTION in a SELECT usually does nothing but piss visitors off. Hence my screaming at the display when things like that are on websites "OH FOR **** SAKE, JUST LET ME TYPE IT IN!" Oh, I'd also consider passing $time as the OPTION's value, since it's a far more useful result. Fancy formatting is for display, not processing.