Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit: forget my first answer, I misunderstood your question. I didn't realise that you want to create a weekly repetition of the records in your timeslot table.</p> <p>So here is a very different solution. It's only partial, but it might help you a bit. Your problem is that you want sorted output. If you loop over the rows in your table in an outer loop, and then loop over the date range in an inner loop, you end up with the wrong sort order.</p> <p>To avoid that, we can switch the loops: loop over the date range in the outer loop, and loop over the timeslots in the inner loop. The problem with that though, is that you should avoid repeatedly requesting the same data from your mysql database, that would slow things down unnecessarily. </p> <p>A solution is to extract the timeslots from the database once, and store it in an array. Then use the date range as the outer loop. The alternative solution is to loop over the rows in your database table in the outer loop, but not echo the results immediately, instead add them to an array and sort the array afterwards. If you store the UNIX dates (not the formatted dates) then sorting is easy. </p> <p>Anyway, here is a possible solution using the first approach. The only remaining bit for you is to create the $timeslots array-of-associative-arrays from your timeslots table.</p> <pre><code># $timeslots is an array of associative arrays. # The trick is to extract this data from your mysql table $timeslots = array( # Timeslot corresponding to Monday 15:00 array ( 'dow' =&gt; 1, 'hour' =&gt; 15 ), # Tuesday 13:00 array ( 'dow' =&gt; 2, 'hour' =&gt; 13 ), ); $date = 1356998400; // '2013-01-01'; $end_date = 1358208000; // '2013-01-15'; while($date &lt;= $end_date) { # Convert the loop variable to day of week $date_dow = date('w', $date); foreach ($timeslots as $timeslot) { # Check if it matches the one in the timeslot if ($date_dow == $timeslot['dow']) { $timeofday = $date + (3600 * $timeslot['hour']); echo date("D, j M Y H:i", $timeofday) . '&lt;br&gt;'; } } $date += 86400; # advance one day } </code></pre> <p>The code above produces the following output:</p> <pre><code>Tue, 1 Jan 2013 13:00 Mon, 7 Jan 2013 15:00 Tue, 8 Jan 2013 13:00 Mon, 14 Jan 2013 15:00 Tue, 15 Jan 2013 13:00 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload