AftabHafeez - 1 month ago 4x
PHP Question

# How to get start and end of the week between two dates

I have two dates

``````\$start_date = '2015-09-21';
\$end_Date = '2016-09-21';
``````

What i want to get like this

``````Week  =  Monday           Friday
36    =  2015-09-21       2015-09-25
37    =  2015-09-28       2015-10-02
38    =  2015-10-05       2015-10-09
.
.
38   =  2016-09-19        2016-09-24
``````

The date difference may be of one year or more. What i need to get is exact each week Monday and Friday dates between this time interval.

I used this method

``````\$weeks = array();
while (\$start_date < \$end_Date )
{
\$weeks[] = date('W', \$start_date );
\$start_date += strtotime('+1 week', 0);
}

function getStartAndEndDate(\$week, \$year)
{

\$time = strtotime("1 January \$year", time());
\$day = date('w', \$time);
\$time += ((7*\$week)+1-\$day)*24*3600;
\$return[0] = date('Y-m-d', \$time);
\$time += 6*24*3600;
\$return[1] = date('Y-m-d', \$time);
return \$return;
}
``````

When i call this function, I need to put week number and year to get exact dates. But I cant get year with the particular week. I can managed to get week from the start date or end date.

``````    \$current_year = date("Y", strtotime(\$fromdate));
``````

Any one suggest me to get the exact year with the week number

First of all you need to convert the time into object, after that calculate the difference. Now calculate the weeks between the given dates. Now its time loop through the weeks from start date, I use `DateInterval('P4D')` to ahead the date by 4 days and then echo two dates then again `DateInterval('P3D')` to complete the week and this is repeated.

``````\$start_date = '2015-09-21';
\$end_Date = '2016-09-21';

\$date1 = new DateTime(\$start_date);
\$date2 = new DateTime(\$end_Date);
\$interval = \$date1->diff(\$date2);

\$weeks = floor((\$interval->days) / 7);

for(\$i = 1; \$i <= \$weeks; \$i++){
\$week = \$date1->format("W");
echo \$week." = ".\$start_date." - ".\$date1->format('Y-m-d')."<br/>";
\$start_date = \$date1->format('Y-m-d');
}
``````

Output:

``````39 = 2015-09-21 - 2015-09-25
40 = 2015-09-28 - 2015-10-02
41 = 2015-10-05 - 2015-10-09
42 = 2015-10-12 - 2015-10-16
43 = 2015-10-19 - 2015-10-23
44 = 2015-10-26 - 2015-10-30
45 = 2015-11-02 - 2015-11-06
46 = 2015-11-09 - 2015-11-13
47 = 2015-11-16 - 2015-11-20
48 = 2015-11-23 - 2015-11-27
49 = 2015-11-30 - 2015-12-04
50 = 2015-12-07 - 2015-12-11
51 = 2015-12-14 - 2015-12-18
52 = 2015-12-21 - 2015-12-25
53 = 2015-12-28 - 2016-01-01
01 = 2016-01-04 - 2016-01-08
02 = 2016-01-11 - 2016-01-15
03 = 2016-01-18 - 2016-01-22
04 = 2016-01-25 - 2016-01-29
05 = 2016-02-01 - 2016-02-05
06 = 2016-02-08 - 2016-02-12
07 = 2016-02-15 - 2016-02-19
08 = 2016-02-22 - 2016-02-26
09 = 2016-02-29 - 2016-03-04
10 = 2016-03-07 - 2016-03-11
11 = 2016-03-14 - 2016-03-18
12 = 2016-03-21 - 2016-03-25
13 = 2016-03-28 - 2016-04-01
14 = 2016-04-04 - 2016-04-08
15 = 2016-04-11 - 2016-04-15
16 = 2016-04-18 - 2016-04-22
17 = 2016-04-25 - 2016-04-29
18 = 2016-05-02 - 2016-05-06
19 = 2016-05-09 - 2016-05-13
20 = 2016-05-16 - 2016-05-20
21 = 2016-05-23 - 2016-05-27
22 = 2016-05-30 - 2016-06-03
23 = 2016-06-06 - 2016-06-10
24 = 2016-06-13 - 2016-06-17
25 = 2016-06-20 - 2016-06-24
26 = 2016-06-27 - 2016-07-01
27 = 2016-07-04 - 2016-07-08
28 = 2016-07-11 - 2016-07-15
29 = 2016-07-18 - 2016-07-22
30 = 2016-07-25 - 2016-07-29
31 = 2016-08-01 - 2016-08-05
32 = 2016-08-08 - 2016-08-12
33 = 2016-08-15 - 2016-08-19
34 = 2016-08-22 - 2016-08-26
35 = 2016-08-29 - 2016-09-02
36 = 2016-09-05 - 2016-09-09
37 = 2016-09-12 - 2016-09-16
``````