2

我只想知道如何获得不包括周末的剩余天数。我尝试减去两个日期,但我似乎无法找到任何消除周末的解决方案。这是我的代码:

            $date_registered = date('Y-m-d');
            $date_planned = $_POST['start_date'];
            $dueDate = $date_registered;
            $numDays = 3;
            $counter = 1;
            while ($counter <= $numDays) {

               $dueDate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($dueDate)) . " +1 day"));
               $dayOfTheWeek = date("l",strtotime($dueDate));

            if ($dayOfTheWeek == "Saturday" || $dayOfTheWeek == "Sunday") {
                   continue;
               }else {
                  $counter++;
               }

            }
            echo $date_registered.'<br>';
            echo $date_planned.'<br>';
            //echo $dueDate;
            $remaining_days = strtotime($date_registered) - strtotime($date_planned);
            echo $remaining_days/86400;

我不知道如何排除周末。我希望你能帮助我。

4

2 回答 2

4

试试这个

$date = date('Y-m-d'); 
$total_days_left = (strtotime($end_date) - strtotime($current_date)) / (60 * 60 * 24);
while (strtotime($date) <= strtotime($end_date)) {
  $timestamp = strtotime($date);  
  $day = date('D', $timestamp); 

  if($day=='Sat' || $day=='Sun') {
    $count++ ; 
  }
  $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}

如果您遇到任何问题,请告诉我。计数将提供这两天之间的周末天数。从中您可以轻松计算剩余天数。

$total_day_left_excluding_weekends = $total_days_left - $count;

请参阅date官方strtotimePHP 网站。

于 2013-02-07T11:08:06.790 回答
0
$time = $sTime = START_TIMESTAMP;
$eTime = END_TIMESTAMP;
$count = 0;

while(date('w', $time) != 0) {
    $time += 86400;
}

while($time < $eTime) {
    $count++;
    $time += 7 * 86400;
}
于 2013-02-07T11:12:56.147 回答