简体   繁体   English

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

[英]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 我想计算从2016MondaysTuesday和结束日期的开始日期

I am trying to have the output 我想要输出

start date("j/n/Y"); 开始 date("j/n/Y"); end date("j/n/Y"); 结束 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. 我已经查看了类似的SO问题,谷歌搜索,但它的特定周#,或不是我想要完成的。

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. 它是有效的,我看起来似乎无法找到我所追求的东西。

You can try following code: 您可以尝试以下代码:

$start_date=date("mdY", strtotime('tuesday this week'));

$end_date=date("mdY", strtotime('monday next week'));

It will give you week starting from Tuesday and ending from Monday . 它会给你从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;
}

You can use the strtotime function to have the first tuesday of january 2016 and then loop 52 times, using next monday , and tomorrow . 您可以使用strtotime函数在2016年1月的第一个星期二,然后循环52次,使用下一个星期一明天 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>";

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM