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 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)