duanhangjian8149 2015-12-18 16:32
浏览 37
已采纳

Laravel&Carbon - 循环数据范围

I would like to list all the day between two dates with Carbon. Here the code:

 @foreach ($rooms as $room)
  <div class="col-sm-4">
        <div class="card">
          <img class="card-img-top" data-src="holder.js/100px180">
          <div class="card-block">
            <h4 class="card-title">{{$room->type}}</h4>
            <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsum reiciendis facere repellat soluta obcaecati laborum ex perspiciatis, iusto.</p>
          </div>
          <ul class="list-group list-group-flush">
            <li class="list-group-item">
                <ul>
                    //////HERE THE PART TO LIST EACH DAY//////
                    @for($d = 0; $d < $checkin->diffInDays($checkout); $d++)
                     <li>{{$checkin->addDay()->toDateString()}}</li>
                    @endfor
                   //////////////////////////////////////////////

                </ul>
            </li>
          </ul>
          <div class="card-block">
             <button type="submit" class="btn btn-primary btn-lg btn-block" name="room_id" value="{{ $room->id }}">Book</button>
          </div>
        </div>
    </div>
    @endforeach

Here is what happens (in this test the $checkin = 2015-12-16 and the $checkout = 2015-12-19):

enter image description here

It is obvious that for each card should be a list with:

  • 2015-12-16
  • 2015-12-17
  • 2015-12-18

I know I could use the php classes (like DatePeriod, DateInterval, ecc..) but I would like to find a good solution with Carbon.

Making some test, it is like Carbon saves the change of the date.

  • 写回答

1条回答 默认 最新

  • dongyingla8668 2015-12-18 17:12
    关注

    You could try to clone the $checkin object like this:

    <?php $date = clone $checkin; ?>
    
    @while ($date->diffInDays($checkout))
        <li>{{$date->toDateString()}}</li>
    
        <?php $date->addDay(); ?>
    @endwhile
    

    Or better:

    @for ($date=clone$checkin; $date->diffInDays($checkout)>0; $date->addDay())
        <li>{{$date->toDateString()}}</li>
    @endfor
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?