Find the week start and end dates of all weeks between two week numbers

I am trying to get the date and start date of all weeks between two weeks.

This is one of my dates 2014-05-17 , and its week number is 20 , and the other date is 2014-08-13 > and its week number is 33 . My task is to get the start and end dates of all weeks between 20 and 33 years. Here Sunday is the beginning of the week and the end of the Saturday week.

$signupweek='2014-05-17'; $signupweek=date("W",strtotime($signupdate)); //week number of current date. $weekNumber = date("W"); 

Can anyone help find the dates.

+5
source share
5 answers

try it

 $signupdate='2014-05-17'; $signupweek=date("W",strtotime($signupdate)); $year=date("Y",strtotime($signupdate)); $currentweek = date("W"); for($i=$signupweek;$i<=$currentweek;$i++) { $result=getWeek($i,$year); echo "Week:".$i." Start date:".$result['start']." End date:".$result['end']."<br>"; } function getWeek($week, $year) { $dto = new DateTime(); $result['start'] = $dto->setISODate($year, $week, 0)->format('Ym-d'); $result['end'] = $dto->setISODate($year, $week, 6)->format('Ym-d'); return $result; } 

Exit

 Week:20 Start date:2014-05-11 End date:2014-05-17 Week:21 Start date:2014-05-18 End date:2014-05-24 Week:22 Start date:2014-05-25 End date:2014-05-31 Week:23 Start date:2014-06-01 End date:2014-06-07 Week:24 Start date:2014-06-08 End date:2014-06-14 Week:25 Start date:2014-06-15 End date:2014-06-21 Week:26 Start date:2014-06-22 End date:2014-06-28 Week:27 Start date:2014-06-29 End date:2014-07-05 Week:28 Start date:2014-07-06 End date:2014-07-12 Week:29 Start date:2014-07-13 End date:2014-07-19 Week:30 Start date:2014-07-20 End date:2014-07-26 Week:31 Start date:2014-07-27 End date:2014-08-02 Week:32 Start date:2014-08-03 End date:2014-08-09 Week:33 Start date:2014-08-10 End date:2014-08-16 
+9
source

Another method ...

If you have a date , from this date you can find the start date and end date this week . But here the week number not used.

For instance:

You have date 2014-08-13 , then start date 2014-08-10 and end date 2014-08-08 are required .

Php code

  $signupweek='2014-8-13'; /*start day*/ for($i = 0; $i <7 ; $i++) { $date = date('Ym-d', strtotime("-".$i."days", strtotime($signupweek))); $dayName = date('D', strtotime($date)); if($dayName == "Sun") { echo "start day is ". $date."<br>"; } } /*end day*/ for($i = 0; $i <7 ; $i++) { $date = date('Ym-d', strtotime("+".$i."days", strtotime($signupweek))); $dayName = date('D', strtotime($date)); if($dayName == "Sat") { echo "end day is ". $date."<br>"; } } 

OUTPUT

 start day is 2014-08-10 end day is 2014-08-16 

Hope this is helpful.

+2
source

Here is an example that implements the function from this answer :

 $signupweek = '2014-05-17'; $signupweek = date("W", strtotime($signupweek)); $current_week = date('W'); $output = array(); // Loop through the weeks BETWEEN your given weeks // to include the start and end week, remove the +1 below and make // it $i <= $current_week for($i = $signupweek + 1; $i < $current_week; $i++) { // Get the start and end for the current week ($i) $dates = getStartAndEndDate($i, '2014'); // if the start or end of the week is greater than now, skip it if(strtotime($dates['start']) > time() or strtotime($dates['end']) > time()) continue; // Add to output array $output[] = $dates; } function getStartAndEndDate($week, $year) { $time = strtotime("1 January $year", time()); $day = date('w', $time); $time += ((7 * $week) + 1 - $day) * 24 * 3600; $return['start'] = date('Yn-j', $time); $time += 6 * 24 * 3600; $return['end'] = date('Yn-j', $time); return $return; } 

Exit

0
source

Try the following:

 $startTime = "2014-05-17"; $startWeek = 20; $endWeek = 33; for ($i = 0; $i <= ($endWeek - $startWeek); $i++) { $days = $i * 7; echo date("Ymd", strtotime($startTime . "+$days day")).'<br />'; } 

Unfortunately, it looks like 2014-08-13 is not the beginning of week 33. 2014-08-16.

0
source

Now you can use DateTime to get the start / end dates of the week (s)

 function getDateRangeForAllWeeks($start, $end){ $fweek = getDateRangeForWeek($start); $lweek = getDateRangeForWeek($end); $week_dates = []; while($fweek['sunday']!=$lweek['sunday']){ $week_dates [] = $fweek; $date = new DateTime($fweek['sunday']); $date->modify('next day'); $fweek = getDateRangeForWeek($date->format("Ymd")); } $week_dates [] = $lweek; return $week_dates; } function getDateRangeForWeek($date){ $dateTime = new DateTime($date); $monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week'); $sunday = clone $dateTime->modify('Sunday this week'); return ['monday'=>$monday->format("Ymd"), 'sunday'=>$sunday->format("Ymd")]; } print_r( getDateRangeForWeek("2016-05-07") ); print_r( getDateRangeForAllWeeks("2015-11-07", "2016-02-15") ); 
0
source

Source: https://habr.com/ru/post/1200179/


All Articles