Nandu
Nandu

Reputation: 3126

Get all week start date and end date within a date range in php

How to get all week start date and end date within a date range in PHP?

Week start = Sunday and week end = Saturday

Input

$start_date='2013-02-01'
$end_date = '2013-02-28'

Output

start date='2013-02-01' End date ='2013-02-02'
start date='2013-02-03' End date ='2013-02-09'
start date='2013-02-10' End date ='2013-02-16'
start date='2013-02-17' End date ='2013-02-23'
start date='2013-02-24' End date ='2013-02-28'

Below code return the week start and end of the date given

 function getWeekDates($date)
{
    $week =  date('W', strtotime($date));
    $year =  date('Y', strtotime($date));
    $from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week
    echo "Start Date-->".$from."End Date -->".$to;//Output : Start Date-->2012-09-03 End Date-->2012-09-09
}

How can I get the result above?

Upvotes: 0

Views: 7997

Answers (3)

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

Try this one...

$start_date = date('Y-m-d', strtotime('2013-02-01'));
$end_date = date('Y-m-d', strtotime('2013-02-28'));
$end_date1 = date('Y-m-d', strtotime('2013-02-28 + 6 days'));

for($date = $start_date; $date <= $end_date1; $date = date('Y-m-d', strtotime($date. ' + 7 days')))
{
    echo getWeekDates($date, $start_date, $end_date);
    echo "\n";
}

function getWeekDates($date, $start_date, $end_date)
{
    $week =  date('W', strtotime($date));
    $year =  date('Y', strtotime($date));
    $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); //Returns the date of monday in week
    if($from < $start_date) $from = $start_date;
    $to = date("Y-m-d", strtotime("{$year}-W{$week}-6"));   //Returns the date of sunday in week
    if($to > $end_date) $to = $end_date;
    echo "Start Date-->".$from."End Date -->".$to;//Output : Start Date-->2012-09-03 End Date-->2012-09-09
}  

See Codepad.

Upvotes: 3

SREE NUNNA
SREE NUNNA

Reputation: 23

my answer.

$start_date = '2013-02-01';
$end_date = '2013-02-28';

getWeekDates($start_date, $end_date);

function getWeekDates($date, $enddate) {
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week
$Edate = strtotime($enddate);
$Sdate = strtotime($to);
if ($Edate <= $Sdate) {
    echo "<br>Start Date-->" . $from . "End Date -->" . $enddate; //Output : Start Date-->2012-09-03 End Date-->2012-09-09

} else {
    echo "<br>Start Date-->" . $from . "End Date -->" . $to; //Output : Start Date-->2012-09-03 End Date-->2012-09-09
    $to = date("Y-m-d", strtotime("$to +1days")); //Returns the date of monday in week
    getWeekDates($to, $enddate);
}
}

Upvotes: 2

TerenceJackson
TerenceJackson

Reputation: 1786

I hope I get your question right.

What you need to do is call your function with the start date, then add seven days to the start date and call your function again... You have to do this until you reach the end date.

Here is some untested code:

$current_date = strtotime($start_date);
$end_date_as_date = strtotime($end_date);

while($current_date < $end_date_as_date){
getWeekDates($current_date);
$current_date= strtotime("+7 day", $current_date);
}

Hope this helps. If you need any further information leave a comment.

Upvotes: 0

Related Questions