Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skoobi's avatar
Level 13

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

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104
// 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();
}
1 like
skoobi's avatar
Level 13

Ah brilliant that works a treat. Been looking through the Carbon docs for a while trying to figure that one out.

How would I do it so it grabs the day of the date instead of manually specifying the Friday?

i.e. if the date is 2018-08-09 which is a Thursday, get Carbon to get the day of the week from that and then get the first Thursday etc

Many thanks

skoobi's avatar
Level 13

Just figured it out. :)

$fromDate = new Carbon($request->get('dateTime'));
$toDate = new Carbon($request->get('reoccurrance_ends_at'));

$dates = [];

for($date = $fromDate; $date->lte($toDate); $date->addWeek()) {
    $dates[] = $date->format('Y-m-d');
}

return $dates;

Please or to participate in this conversation.