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
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
Ok thanks to Mysterious answer I got it:
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, $dateInterval, $endDate);
return View::make('backend/menubuilder/edit')->with('id',$id)->withDateperiod($dateperiod);
this displays the entire current week from monday to sunday Thanks again