0

所以,我有这个功能:

function calculateNextDate($startDate, $days)
{
    $dateTime = new DateTime($startDate);

        while($days) {
            $dateTime->add(new DateInterval('P1D'));    

            if ($dateTime->format('N') < 6) {
                $days--;
            }
        }

    return $dateTime->format('Y-m-d');
}

如果我像这样运行我的函数:

echo calculateNextDate('2012-10-01', '10');

这将导致:

2012-10-15

这是正确的,但是我希望它算作$startDate一个计数日,所以它会像这样计数:

1. 2012-10-01
2. 2012-10-02
3. 2012-10-03
4. 2012-10-04
5. 2012-10-05
6. 2012-10-08
7. 2012-10-09
8. 2012-10-10
9. 2012-10-11
10. 2012-10-12

可能吗?

4

3 回答 3

1

先减去一天

function calculateNextDate($startDate, $days)
{
  $oneDay = new DateInterval('P1D');
  $dateTime = new DateTime($startDate);
  $dateTime->sub($oneDay);

  while($days) {
    $dateTime->add($oneDay);
    if ($dateTime->format('N') < 6) {
      $days--;
    }
  }

  return $dateTime->format('Y-m-d');
}

echo calculateNextDate('2012-10-01', 10);
// results in 2012-10-12
于 2012-10-15T07:49:25.873 回答
1
<?
function calculateNextDate($startDate, $days)
{
    $dateTime = new DateTime($startDate);
        while($days > 0) {
            $weekend = date('w', strtotime($dateTime->format('Y-m-d')));
            if($weekend != '6' && $weekend != '0'){
                $new_date[] = $dateTime->format('Y-m-d');
                $days--;
            }
            $dateTime->add(new DateInterval('P1D'));    
        }
    return $new_date;
}

echo "<pre>";
print_r(calculateNextDate('2012-10-11', '10'));
?>

结果将是:

Array
(
    [0] => 2012-10-11
    [1] => 2012-10-12
    [2] => 2012-10-15
    [3] => 2012-10-16
    [4] => 2012-10-17
    [5] => 2012-10-18
    [6] => 2012-10-19
    [7] => 2012-10-22
    [8] => 2012-10-23
    [9] => 2012-10-24
)

您可以通过循环数组来回显日期。

于 2012-10-15T07:47:07.460 回答
0

嗯,非常复杂的方法,只需添加开始日期的天数

我会这样做

$resultingDate = date('Y-m-d', strtotime("+$days days", $startDate));
于 2012-10-15T07:33:08.950 回答