Reputation: 173
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
Upvotes: 0
Views: 2868
Reputation: 11
The answer provided by 'Yogesh Singasane' won't work if the start date is 1st Jan and it ends up in week 52 of the previous year. Here is the modified code that seems to be covering the same corner case:
$start_date = '2023-01-01';
$end_Date = '2023-01-31';
$startTime = strtotime($start_date);
$endTime = strtotime($end_Date);
$weeks = array();
$date = new DateTime();
$i=0;
while ($startTime < $endTime) {
$weeks[$i]['week'] = date('W', $startTime);
if(date("m-d", strtotime($start_date)) == '01-01' && date('W', $startTime) >= 52){
$weeks[$i]['year'] = date('Y', $startTime) - 1 ;
}
else{
$weeks[$i]['year'] = date('Y', $startTime);
}
$date->setISODate($weeks[$i]['year'], $weeks[$i]['week']);
$weeks[$i]['Monday']=$date->format('Y-m-d');
$weeks[$i]['Friday'] = date('Y-m-d',strtotime($weeks[$i]['Monday'] . "+4 days"));
$startTime += strtotime('+1 week', 0);
$i++;
}
var_dump($weeks);
P.S. Google brought me here, and I wanted to post to help others like me.
Upvotes: 1
Reputation: 273
$start_date = '2015-09-21';
$end_Date = '2016-09-21';
$startTime = strtotime($start_date);
$endTime = strtotime($end_Date);
$weeks = array();
$date = new DateTime();
$i=0;
while ($startTime < $endTime) {
$weeks[$i]['week'] = date('W', $startTime);
$weeks[$i]['year'] = date('Y', $startTime);
$date->setISODate($weeks[$i]['year'], $weeks[$i]['week']);
$weeks[$i]['Monday']=$date->format('Y-m-d');
$weeks[$i]['Friday'] = date('Y-m-d',strtotime($weeks[$i]['Monday'] . "+4 days"));
$startTime += strtotime('+1 week', 0);
$i++;
}
var_dump($weeks);
Upvotes: 0
Reputation: 9583
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");
$date1->add(new DateInterval('P4D'));
echo $week." = ".$start_date." - ".$date1->format('Y-m-d')."<br/>";
$date1->add(new DateInterval('P3D'));
$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
Upvotes: 6