PHP获取特定年份的52周开始/结束日期


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

我想计算Tuesday的开始日期和在2016 年降落在Mondays的结束日期

我正在尝试输出

开始date("j/n/Y");结束date("j/n/Y");

我看过类似的SO问题,也在谷歌上搜索过,但这是针对特定的一周,或者不是我想要完成的。

我想要得到的是以下内容。

    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
... 

等等,索沃斯。

任何帮助都将不胜感激,并希望人们不要对这个问题投反对票。它是有效的,我看了看,似乎找不到我想要的东西。

您可以使用strtotime函数进行2016年1月的第一个星期二,然后循环52次,使用下周一明天。所以你不需要做任何计算。

 <?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>";

您可以尝试以下代码:

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

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

它将为您提供从Tuesday开始到Monday结束的week

$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;
}

当日期小于您定义的结束日期时,每周循环。

$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'));
}