0

I have a simple table with Location, Start Date and End Date.

These are school terms so the range is about 12 to 14 weeks and don't cross the end of year.

I need to calculate and display all the dates (dd/mm) of the Mondays between the dates?

The start and end date may or may not be Mondays.

4
  • 1
    What have you tried? Read www.stackoverflow.com/help/how-to-ask for tips on how to ask proper questions. We will not create entire solutions for you. Commented Mar 10, 2016 at 8:18
  • The problem is, I don't know where to start, I just need pointing in the right direction? I can code happily in CI, but it's the process of how to do this that is evading me? Commented Mar 10, 2016 at 8:26
  • 1
    There are many ways to do this. There are many libraries available to help you evaluate dates as well. Commented Mar 10, 2016 at 8:29
  • Is this to be performed in SQL or PHP? There is no coding attempt, so we don't know how much or where to code. Commented Jul 8 at 3:49

3 Answers 3

2

You can try converting the datestart and end then for loop them. As you loop them use the date() to check if it is Monday. date() manual is here.

<?php
$date_from = strtotime("2016-03-01");
$date_to = strtotime("2016-04-30");

$oneDay = 60*60*24;

for($i=$date_from; $i<=$date_to; $i=$i+$oneDay)
{
    if (date('N', $i) == 1) { //date('N') 1 = Monday, 2 = Tuesday....
        echo date("D", $i) ." ". date('Y-m-d', $i) ."<br>";
    }
}
?>

Output:

Mon 2016-03-07
Mon 2016-03-14
Mon 2016-03-21
Mon 2016-03-28
Mon 2016-04-04
Mon 2016-04-11
Mon 2016-04-18
Mon 2016-04-25
Sign up to request clarification or add additional context in comments.

2 Comments

Thanx, I'll give it a go :)
Works a treat, thanx :) I can't score you yet as I don't have enough reputation, but I would have done :)
0

I produced this code which displays the dates along the top of the page.

Reading all details from the table.

$datesrow = $dates->row();
$date_from = strtotime($datesrow->StartDate);
$date_to = strtotime($datesrow->EndDate);
$oneDay = 60*60*24;
echo '<h2>Register</h2>';
echo '<table>';
echo '<tr><td class="register">&nbsp;</td><td class="register">';
for($i=$date_from; $i<=$date_to; $i=$i+$oneDay)
{
    if (date('N', $i) == $this->session->userdata('DaysID')-100):
        echo date('d/m', $i) .'</td><td class="register">';
    endif;
}
echo '</td></tr>';

Comments

0

Since you wrote that you are using MySql you can retrive all of the Mondays in all the date ranges that exist in your table (aka "Start Date" and "End Date") , you can do it by using the following Query:

select DISTINCT * from 
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date from
 (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v,
 `Tests`
where selected_date between `Tests`.`startDate` and `Tests`.`endDate` and DAYOFWEEK(selected_date)=2

you can test it in sqlfiddle: http://sqlfiddle.com/

use the following schema:

CREATE TABLE Tests
    (`id` int, `startDate` DATE,`endDate` DATE)
;


INSERT INTO Tests
    (`id`, `startDate`, `endDate`)
VALUES
    (1, '2016-03-6', '2016-03-22'),
    (2, '2016-03-6', '2016-03-22')
;

and verify with the query above.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.