Khirad Zahra
Khirad Zahra

Reputation: 893

How to calcuate (n) no of coming saturdays with Laravel and Carbon

How can i get the coming (n) Saturdays form a specific date.

Like if i want to calculate 6 Saturdays from now these will be like so.

What i have tried so for is

$dateDay = \Carbon\Carbon::now();
$year = $dateDay->year;
$month = $dateDay->month;
$days = $dateDay->daysInMonth;
$saturdays=[];
foreach (range(1, $days) as $day) {
    $date = \Carbon\Carbon::createFromDate($year, $month, $day);
    if ($date->isSaturday()===true) {
        $saturdays[]=($date->day);
    }
}
print_r($saturdays);

but this only shows Saturdays of the current month.

Upvotes: 1

Views: 450

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Just use Carbon. This will return a Carbon object with 2017-05-06 date:

Carbon::parse('first saturday')->addWeeks(5);

Upvotes: 2

Related Questions