duanpa2143 2013-02-07 11:03
浏览 43
已采纳

剩余的日子不包括周末

i just want to know how to get the remaining days excluding the weekends. i tried subtracting two dates but i cant seem to find any solutions on removing weekends. well this is my code:

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

i dont have any idea how to exclude the weekends .i hope you could help me.

  • 写回答

2条回答 默认 最新

  • doupo2157 2013-02-07 11:08
    关注

    Try this one

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

    Let me know if you face any issue. Count will provide the number of week end days falling between these two days.From that you can count remaining day easily.

    $total_day_left_excluding_weekends = $total_days_left - $count;
    

    Refer date and strtotime on official PHP site.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥30 关于#wpf# devexpress的问题:选中时是黑色未选中是白色字体,想要更改未选中时字体的颜色(语言-c#)
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常