0

For example, today is Tuesday, 22-12-2012 For entry I have Wednesday and the number three. I want these dates...

2021-12-23
2021-12-29
2022-01-05

echo \Carbon\Carbon::now()->next('Wednesday')->toDateString();

i want replace the Wednesday to the number, Because my input is Persian.

$numberWeek = [
    'شنبه' => 0,
    'یکشنبه' => 1,
    'دوشنبه' => 2,
    'سه شنبه' => 3,
    'چهارشنبه' => 4,
    'پنجشنبه' => 5,
    'جمعه' => 6,
];

Can anyone help me?

Thanks everybody

2 Answers2

1

There is multiple ways. One of them

echo \Carbon\Carbon::now()->next('Wednesday')->toDateString();
echo \Carbon\Carbon::now()->next('Wednesday')->addWeeks(1)->toDateString();
echo \Carbon\Carbon::now()->next('Wednesday')->addWeeks(2)->toDateString();

If you want to have it as loop:

$date = '2021-12-22';
$number = 3;
$i = 0;

while ($i < $number) {
    echo \Carbon\Carbon::parse($date)->next('Wednesday')->addWeeks($i)->toDateString()."\n";
    $i++;
}
Autista_z
  • 2,491
  • 15
  • 25
  • Is there a way to use numbers instead of Wednesday? Our calendar is Persian, for example, send the number 4 for the Wednesday. $numberWeek = [ 'شنبه' => 0, 'یکشنبه' => 1, 'دوشنبه' => 2, 'سه شنبه' => 3, 'چهارشنبه' => 4, 'پنجشنبه' => 5, 'جمعه' => 6, ]; – Mohammad Afshar Dec 21 '21 at 20:30
  • @MohammadAfshar , that should work as well. I.e: `echo \Carbon\Carbon::now()->next(3)->toDateString();` Keep in mind that [*Day of week numbers* are **zero-indexed** starting from *Sunday*](https://carbon.nesbot.com/docs/#:~:text=Day%20of%20week%20number%20%28from%200%20%28Sunday%29%20to%206%20%28Saturday%29%29%2C%20similar%20to%20%22d%22%20but%20this%20one%20is%20translatable%20%28takes%20first%20day%20of%20week%20of%20the%20current%20locale%29) – steven7mwesigwa Dec 21 '21 at 20:48
  • 1
    Another problem with this code , if today is Wednesday, it will not register today – Mohammad Afshar Dec 21 '21 at 20:49
  • 1
    @MohammadAfshar , In that case, you may want to register the actual *timezone* (`->setTimezone("+3:30")`) in your computation. I.e: `echo \Carbon\Carbon::now()->setTimezone("+03:30")->next(3)->toDateString();` – steven7mwesigwa Dec 21 '21 at 21:16
0

Another method is mentioned below.

$now = Carbon::now();

for ($i = 0; $i < 3; $i++) {
    echo $now->addWeeks($i)->weekdays(3)->format('Y-m-d');
}
Sevan
  • 669
  • 1
  • 5
  • 18