Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get closest date by day of week?

Tags:

date

php

laravel

I have the weekdays from 1 to 7 (Monday to Sunday).

I want to get the next date, where the day is the specified day (from 1 to 7).

For example, today is 25.09.2018.

When I pass 5 (Friday) I want to get 28.09.2018 which is the next closest Friday.

How can I achieve it? Thanks in advance!

like image 320
Codearts Avatar asked Sep 25 '18 13:09

Codearts


1 Answers

Example for Friday(5)

Carbon::now()
    ->next(5)
    ->toDateString();

More about Carbon library

like image 173
Egretos Avatar answered Sep 22 '22 23:09

Egretos