Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

php

I am trying to get the start date and end dates of all weeks between two week numbers.

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

$signupweek='2014-05-17';
$signupweek=date("W",strtotime($signupdate));

//week number of current date.
$weekNumber = date("W");

Can anyone help to find the dates.

like image 790
Aishwaryas Avatar asked Aug 13 '14 04:08

Aishwaryas


2 Answers

try this

$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('Y-m-d');
  $result['end'] = $dto->setISODate($year, $week, 6)->format('Y-m-d');
  return $result;
}

Output

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
like image 97
noufalcep Avatar answered Oct 16 '22 13:10

noufalcep


Another method...

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

For example:

You have a date 2014-08-13, then required start date is 2014-08-10 and end date is 2014-08-16.

PHP code is

   $signupweek='2014-8-13';
/*start day*/
    for($i = 0; $i <7 ; $i++)
    {
     $date = date('Y-m-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('Y-m-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 useful..

like image 43
Deepu Sasidharan Avatar answered Oct 16 '22 12:10

Deepu Sasidharan