2013-02-07 108 views
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

試試這個

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

官方PHP網站參閱datestrtotime

+0

工作與否? – Deep123

+0

以及它的工作。我似乎無法正確得到剩餘的日子 – arukiri123

+0

你現在得到了嗎?還是還沒有? – Deep123

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

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

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

1天!= 24小時,1天!= 86400秒,請改用strtotime()! – HamZa

相關問題