duanhe1976 2016-04-20 09:17
浏览 20

Laravel4碳循环一周

I am trying to loop trough the current week with carbon and display every single day in the view.

$current_mon and $current_sun is a dropdown to select the current week from monday to sunday. I have no idea how to get the rest of the week (tue to sat) as

    $tues = Carbon::now()->startOfWeek()->addDay(); 

is a bad idea!

My Controller is:

$now = Carbon::now();
    $current_mon = Carbon::now()->startOfWeek();        
    $current_sun = Carbon::now()->endOfWeek()->format('d-M');
    $tues = Carbon::now()->startOfWeek()->addDay(); 
    return View::make('backend/menubuilder/edit')->with('id',$id)->withCurrent_mon($current_mon)->withCurrent_sun($current_sun)->withTues($tues);

Please advice

  • 写回答

2条回答 默认 最新

  • duancan1900 2016-04-20 09:44
    关注

    Make use of DateInterval and DatePeriod.

    Controller:

    $startDate = \Carbon::now()->startOfWeek();        
    $endDate = \Carbon::now()->endOfWeek();
    //Init interval
    $dateInterval = \DateInterval::createFromDateString('1 day');
    //Init Date Period from start date to end date
    //1 day is added to end date since date period ends before end date. See first comment: http://php.net/manual/en/class.dateperiod.php
    $datePeriod = new \DatePeriod($startDate, $interval, $endDate->modify('+1 day'));
    
    return View::make('backend/menubuilder/edit',[$datePeriod]);
    

    View:

    @foreach($datePeriod as $datePeriodRow)
        {!! DateTime Object: See: http://php.net/manual/en/class.datetime.php !!}
        {{$datePeriodRow->format('d-M')}}
    @endforeach
    
    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程