LeviZoesch
LeviZoesch

Reputation: 1621

PHP Get 52 week start/end date for a specific year

I would like to calculate the start date from Tuesday and the end date to land on Mondays in the year 2016

I am trying to have the output

start date("j/n/Y"); end date("j/n/Y");

I have looked at similar SO questions, and google searches but its for specific week #'s, or not what I am trying to accomplish.

What I am trying to get is the following.

    Start: 12/14/2015  
End 12/21/2015 
    Start: 12/22/2015  
End 12/28/2015

    ....

    Start: 01/05/2016  
 End 01/11/2016
    Start: 01/12/2016  
 End 01/18/2016

... 

so on and soforth.

Any help would be greatly appreciated, and hope people dont downvote the crap out of this question. it is valid, and I have looked and cannot seem to find what I am after.

Upvotes: 1

Views: 895

Answers (4)

Adam
Adam

Reputation: 18855

You can use the strtotime function to have the first tuesday of january 2016 and then loop 52 times, using next monday, and tomorrow. So you don't have to do any calculation.

 <?php
 $ts=strtotime("tuesday january 2016");
 echo "<table>";
 for ($i=0;$i<52;$i++) {
    echo "<tr><td>Start: ".date("Y/m/d", $ts);
    $ts=strtotime("next monday", $ts);
    echo "</td><td>End: ".date("Y/m/d", $ts)."</td>\n";
    echo "</td><td>Payweek: ".date("W", $ts)."</td></tr>\n";
    $ts=strtotime("tomorrow", $ts);
 }
 echo "</table>";

Upvotes: 1

CodeBoy
CodeBoy

Reputation: 3300

$oneWeek = 7*24*60*60;   // 1 week in seconds
$curr_ts = time();
while ($curr_ts < strtotime('2017-01-01')) {
    echo "    Start: ",
        date('m/d/Y', strtotime('tuesday this week', $curr_ts)),
        "\nEnd ",
        date('m/d/Y', strtotime('monday next week', $curr_ts)),
        "\n";
    $curr_ts = $curr_ts + $oneWeek;
}

Upvotes: 1

JRD
JRD

Reputation: 1987

While the date is less than your defined end date, loop over each week.

$date = Some start date ...;
$endDate = Some end date ...;

while (date_interval_format($date->diff($endDate),'%R') == '+') {
    echo $date->format('j/n/Y') . " - " . $date->add(new DateInterval('P6D'))->format('j/n/Y') . "\n";
    $date->add(new DateInterval('P1D'));
}

Upvotes: 0

Bijal
Bijal

Reputation: 132

You can try following code:

$start_date=date("m-d-Y", strtotime('tuesday this week'));

$end_date=date("m-d-Y", strtotime('monday next week'));

It will give you week starting from Tuesday and ending from Monday.

Upvotes: 1

Related Questions