剩余的日子不包括周末

时间:2013-02-07 11:03:55

标签: php mysql

我只是想知道除了周末之外的剩余天数。我试着减去两个日期,但我似乎找不到任何解决方案去除周末。这是我的代码:

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

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

2 个答案:

答案 0 :(得分: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)));
}

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

$total_day_left_excluding_weekends = $total_days_left - $count;

在官方PHP网站上提及datestrtotime

答案 1 :(得分:0)

$time = $sTime = START_TIMESTAMP;
$eTime = END_TIMESTAMP;
$count = 0;

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

while($time < $eTime) {
    $count++;
    $time += 7 * 86400;
}