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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化