dtkvlj5386 2019-06-06 21:14
浏览 71
已采纳

如何在特定日期添加天数并跳过星期日并在其他日子继续?

I'm trying to create an array of dates. The idea is that I add a number x days and the code, when adding the days skip only Sunday.

This is for laravel and I'm using carbon.

$date = Carbon::now();
   $dates = [];

   for($i = 1 ; $i < 20; $i++){


    if($date->dayOfWeek === Carbon::SATURDAY){

        echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . " - Sunday <br> ";


    } else {


        echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . "<br>";

    }

When i use the constant SUNDAY to skip this date, its not working. It goes on to consider Sunday as Monday

  • 写回答

2条回答 默认 最新

  • dousuo8400 2019-06-07 15:05
    关注

    The problem is that you are checking if it's Saturday, and after that you're adding a day to it. You need to echo the date before you add a day to it.

    Try this:

    if($date->dayOfWeek === Carbon::SUNDAY){ // checking if the current date is a sunday
        echo $dates[$i] = $date->format('d/m/Y') . " - Sunday <br> "; // echo and add the current date to the array
        $date->addDay(1);
    } else {
        echo $dates[$i] = $date->format('d/m/Y') . "<br>"; // echo and add the current date to the array
        $date->addDay(1);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?