searls03 Posted May 28, 2014 Share Posted May 28, 2014 Hi, I am completely stuck on this. I am trying to allow the user to select a start day and end day. then the system adds two weeks to the start day and each previous calculation (ie intervals of 0 weeks, 2 weeks, 4 weeks, etc. from the start day) until the end date is reached. I would also like each of these dates to be inserted into my database. I have no clue as to where to even start with this. I know I will need some kind of loop based on days, but I am not sure how I would write this. I suppose this would be the basic structure of the query, but not sure how to incorporate this loop that I need. mysql_query("INSERT INTO events (title, date) VALUES ('$title', '(current date that the loop is in)')"); mysql_error(); can anyone help me out with this? thanks in Advance! Quote Link to comment Share on other sites More sharing options...
searls03 Posted May 28, 2014 Author Share Posted May 28, 2014 and this is in a PHP insert query btw. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 28, 2014 Share Posted May 28, 2014 the php datetime functions has a DatePeriod class that allows iteration over a set of dates - $start = '2014-05-01'; $end = '2014-07-01'; $start_date = new DateTime($start); $end_date = new DateTime($end); $end_date = $end_date->modify('+1 day'); // if you need the end date in the range (DatePeriod is not inclusive of the end date) $interval = new DateInterval("P14D"); // 14 days $dates = new DatePeriod($start_date, $interval, $end_date); // Traversable date range foreach($dates as $date){ echo $date->format('Y-m-d') . '</br>'; } Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 28, 2014 Share Posted May 28, 2014 Or just use a for or while loop: <?php $start = '2014-05-01'; $end = '2014-07-01'; $current_date = new DateTime($start); $end_date = new DateTime($end); while ($current_date <= $end_date) { echo $current_date->format('Y-m-d'), '<br>'; $current_date->modify('+2 weeks'); } Quote Link to comment 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.