// Get the date range
$fromDate = new Carbon($request->get('dateTime'));
$toDate = new Carbon($request->get('reoccurance_ends_at'));
// Get the first Friday in the date range
$date = $fromDate->dayOfWeek == Carbon\Carbon::FRIDAY
? $fromDate
: $fromDate->copy()->modify('next Friday');
$dates = [];
// Iterate until you have reached the end date adding a week each time
while ($date->lt($toDate)) {
$dates[] = $date->toDateString();
$date->addWeek();
}
Getting dates for each friday in date range
Hi. I'm trying to use carbon to get the date of a given weekday (i.e. Friday) and then depending on the date range, return the list of dates of those given days.
So for example, if my date range was this month and I wanted the dates for each Friday of this month, id want it to come back with::
2018-08-03,
2018-08-10,
2018-08-17,
2018-08-24,
2018-08-31
What I'm trying to do is once someone creates a booking and they select reoccurring it then drops down to choose weekly or monthly along with an end date.
I was then wanting it to get the number of weeks in that range and add a booking in the database for each week up until that range with the correct date.
I've managed to get the difference between the range, but am struggling to get to the next level.
if($request->get('reoccuring') == 1){
// Get the date range from x to y
// get the dates of each of the reoccuring days
// save to database as seperate bookings with the new date on that day
// Get the date range
$fromDate = new Carbon($request->get('dateTime'));
$toDate = new Carbon($request->get('reoccurance_ends_at'));
// Get Frequency
if($request->get('reoccuring_frequency') == 'weekly'){
$date = $fromDate->diffInWeeks($toDate);
return $date;
}
} else {
$booking = new Booking;
$booking->name = $request->get('name');
$booking->email = $request->get('email');
$booking->phone = $request->get('phone');
$booking->dateTime = $request->get('dateTime');
$booking->reoccuring = $request->get('reoccuring');
$booking->event_type = $request->get('event_type');
$booking->data_consent = 0;
$booking->paid = $request->get('paid');
$booking->price = $request->get('price');
$booking->approved = $request->get('approved');
$booking->status = 1;
$booking->save();
}
Any help or info would be great.
Thank you
Please or to participate in this conversation.