I'm using Laravel and Carbon.
I create a function to add 'availability' like:
public function createAvailability(Request $request) {
$availability = new Availability($request->all());
$availability->save();
return Redirect::back();
}
I send 'start' and 'end' data to request, so example data is: $request->start = '01/07/2018'; $request->end= '22/07/2018';
and now I insert in database data like:
What I want to do is to insert data in a database like:
so for the first 7 days, I want to insert names with the same 'start' and 'end' data... or if start date is 01/07 and end date is 03/07 I want to insert just 3 days with day of week names...
How I can do that?
Also here is my Availablity class with protected dates:
class Availability extends Model
{
protected $dates = [ 'start','end' ];
public function setStartAttribute($date) {
$this->attributes['start']= Carbon::createFromFormat('d/m/Y', $date);
}
public function getStartAttribute($date){
return (new Carbon($date))->format('d-m-Y');
}
public function setEndAttribute($date) {
$this->attributes['end']= Carbon::createFromFormat('d/m/Y', $date);
}
public function getEndAttribute($date){
return (new Carbon($date))->format('d-m-Y');
}
}
I think this may be what you are wanting
function days($start, $end){
$current = strtotime($start);
$end = strtotime($end);
while($current <= $end && $current <= ($current * 7))){ // go until the last day or seven days, whichever comes first
$day = date("l", $current);
$availability = new Availability();
$availability->start_date = date('d/m/Y', $current);
$availability->end_date = date('d/m/Y', $end);
$availability->day_of_week = date("l", $current);
$availability->save();
$current = $current + 86400;
}
days($request->input('start'), $request->input('end'));