查找两周数字之间所有周的周开始和结束日期

时间:2014-08-13 04:35:19

标签: php date

我正在尝试获取两周数字之间所有周的开始日期和结束日期。

这是我的约会之一 2014-05-17 ,其周数 20 ,其他日期 2014-08-13 及其周数 33 。 我的任务是获取20到33之间所有周的开始和结束日期。这里的星期日是星期开始和星期六结束。

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

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

任何人都可以帮忙找到日期。

5 个答案:

答案 0 :(得分:11)

试试这个

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

输出

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

答案 1 :(得分:3)

另一种方法......

如果您有date,则从该日期开始,您可以找到start date的{​​{1}}和end date。但是这里没有使用week

例如:

您有week number 2014-08-13 ,然后必需date 2014-08-10 start date 2014-08-16

PHP代码

end date

<强>输出

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

希望这很有用..

答案 2 :(得分:0)

以下是从this answer实现功能的示例:

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

$current_week = date('W');

$output = array();
// Loop through the weeks BETWEEN your given weeks
// to include the start and end week, remove the +1 below and make 
// it $i <= $current_week
for($i = $signupweek + 1; $i < $current_week; $i++) {
    // Get the start and end for the current week ($i)
    $dates = getStartAndEndDate($i, '2014');
    // if the start or end of the week is greater than now, skip it
    if(strtotime($dates['start']) > time() or strtotime($dates['end']) > time())
        continue;
    // Add to output array
    $output[] = $dates;
}

function getStartAndEndDate($week, $year)
{

    $time = strtotime("1 January $year", time());
    $day = date('w', $time);
    $time += ((7 * $week) + 1 - $day) * 24 * 3600;
    $return['start'] = date('Y-n-j', $time);
    $time += 6 * 24 * 3600;
    $return['end'] = date('Y-n-j', $time);
    return $return;
}

Output

答案 3 :(得分:0)

试试这个:

$startTime = "2014-05-17";
$startWeek = 20;
$endWeek = 33;

for ($i = 0; $i <= ($endWeek - $startWeek); $i++) {
    $days = $i * 7;
    echo date("Y-m-d", strtotime($startTime . "+$days day")).'<br />';
}

不幸的是,2014-08-13似乎不是第33周的开始.2014-08-16是。

答案 4 :(得分:0)

您现在可以使用DateTime获取周的开始/结束日期

function getDateRangeForAllWeeks($start, $end){
    $fweek = getDateRangeForWeek($start);
    $lweek = getDateRangeForWeek($end);
    $week_dates = [];
    while($fweek['sunday']!=$lweek['sunday']){
        $week_dates [] = $fweek;
        $date = new DateTime($fweek['sunday']);
        $date->modify('next day');

        $fweek = getDateRangeForWeek($date->format("Y-m-d"));
    }
    $week_dates [] = $lweek;

    return $week_dates;
}

function getDateRangeForWeek($date){
    $dateTime = new DateTime($date);
    $monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week');
    $sunday = clone $dateTime->modify('Sunday this week'); 
    return ['monday'=>$monday->format("Y-m-d"), 'sunday'=>$sunday->format("Y-m-d")];
}

print_r( getDateRangeForWeek("2016-05-07") );

print_r( getDateRangeForAllWeeks("2015-11-07", "2016-02-15") );